我正在实现一个隐藏的搜索栏。
在隐藏搜索栏的功能中,所有命令都可以正常运行,但通过命令带回该栏按钮的命令除外。
另外,UISearchBar.showCancelButton也无法正常工作,因此我不得不手动实现取消按钮。 (我已将搜索栏的委托设置为self)
这是代码:
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()
}
我该如何解决这些问题?
最佳答案
事实证明,那些条形按钮项为零,我将出口更改为强壮,现在可以使用了。
从:
@IBOutlet var weak searchButton: UIBarButtonItem!
@IBOutlet var weak menuButton: UIBarButtonItem!
至:
@IBOutlet var searchButton: UIBarButtonItem!
@IBOutlet var menuButton: UIBarButtonItem!
关于ios - navigationItem.set_BarButton不起作用-swift,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/51021051/