navigationItem.set_BarButton 不起作用 - swift
navigationItem.set_BarButton does not work - swift
我正在实施一个隐藏的搜索栏。
一切正常,除了在隐藏搜索栏的函数中恢复栏按钮的命令。
此外,UISearchBar.showCancelButton 也不起作用,所以我不得不手动实现取消按钮。 (我已经将搜索栏的委托设置为自己)
代码如下:
func dismissSearch() {
searchBar.text = ""
view.endEditing(true)
tagSearchResults = [Tag]()
TagSearchFeed?.collectionView.reloadData()
searchResultsContainer.isHidden = true
hideSearchBar()
}
func showSearchBar() {
searchBar.alpha = 0
navigationItem.titleView = searchBar
let cancelButton = UIBarButtonItem(title: "Cancel", style: .plain, target: self, action: #selector(searchBarCancelButtonClicked))
cancelButton.tintColor = .white
navigationItem.setRightBarButton(cancelButton, animated: true)
navigationItem.setLeftBarButton(nil, animated: true)
UIView.animate(withDuration: 0.5, animations: {
self.searchBar.alpha = 1
}, completion: { finished in
self.searchBar.becomeFirstResponder()
})
self.searchResultsContainer.isHidden = false
}
func hideSearchBar() {
navigationItem.setRightBarButton(self.searchButton, animated: true)
navigationItem.setLeftBarButton(self.menuButton, animated: true)
UIView.animate(withDuration: 0.3, animations: {
self.navigationItem.titleView = nil
}, completion: { finished in
})
}
@objc func searchBarCancelButtonClicked() {
dismissSearch()
}
我该如何解决这些问题?
原来那些bar buttons item是nil,我把outlets改成了strong,现在可以用了。
来自:
@IBOutlet var weak searchButton: UIBarButtonItem!
@IBOutlet var weak menuButton: UIBarButtonItem!
收件人:
@IBOutlet var searchButton: UIBarButtonItem!
@IBOutlet var menuButton: UIBarButtonItem!
我正在实施一个隐藏的搜索栏。
一切正常,除了在隐藏搜索栏的函数中恢复栏按钮的命令。
此外,UISearchBar.showCancelButton 也不起作用,所以我不得不手动实现取消按钮。 (我已经将搜索栏的委托设置为自己)
代码如下:
func dismissSearch() {
searchBar.text = ""
view.endEditing(true)
tagSearchResults = [Tag]()
TagSearchFeed?.collectionView.reloadData()
searchResultsContainer.isHidden = true
hideSearchBar()
}
func showSearchBar() {
searchBar.alpha = 0
navigationItem.titleView = searchBar
let cancelButton = UIBarButtonItem(title: "Cancel", style: .plain, target: self, action: #selector(searchBarCancelButtonClicked))
cancelButton.tintColor = .white
navigationItem.setRightBarButton(cancelButton, animated: true)
navigationItem.setLeftBarButton(nil, animated: true)
UIView.animate(withDuration: 0.5, animations: {
self.searchBar.alpha = 1
}, completion: { finished in
self.searchBar.becomeFirstResponder()
})
self.searchResultsContainer.isHidden = false
}
func hideSearchBar() {
navigationItem.setRightBarButton(self.searchButton, animated: true)
navigationItem.setLeftBarButton(self.menuButton, animated: true)
UIView.animate(withDuration: 0.3, animations: {
self.navigationItem.titleView = nil
}, completion: { finished in
})
}
@objc func searchBarCancelButtonClicked() {
dismissSearch()
}
我该如何解决这些问题?
原来那些bar buttons item是nil,我把outlets改成了strong,现在可以用了。 来自:
@IBOutlet var weak searchButton: UIBarButtonItem!
@IBOutlet var weak menuButton: UIBarButtonItem!
收件人:
@IBOutlet var searchButton: UIBarButtonItem!
@IBOutlet var menuButton: UIBarButtonItem!