navigationItem.set_BarButton 不起作用 - 迅速
Posted
技术标签:
【中文标题】navigationItem.set_BarButton 不起作用 - 迅速【英文标题】:navigationItem.set_BarButton does not work - swift 【发布时间】:2018-06-25 10:15:20 【问题描述】:我正在实现一个隐藏的搜索栏。
一切正常,除了在隐藏搜索栏的功能中恢复条形按钮的命令。
另外,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()
我该如何解决这些问题?
【问题讨论】:
【参考方案1】:原来那些条形按钮项是 nil,我将 outlets 更改为 strong,现在它可以工作了。 来自:
@IBOutlet var weak searchButton: UIBarButtonItem!
@IBOutlet var weak menuButton: UIBarButtonItem!
收件人:
@IBOutlet var searchButton: UIBarButtonItem!
@IBOutlet var menuButton: UIBarButtonItem!
【讨论】:
以上是关于navigationItem.set_BarButton 不起作用 - 迅速的主要内容,如果未能解决你的问题,请参考以下文章