使用Back on Navigation Bar错误UIBarButtonItem

时间:2016-02-19 17:12:13

标签: ios swift uitableview uinavigationcontroller uibarbuttonitem

我在TableView的顶部有一个NavigationBar。打开/关闭搜索看起来很不错。

enter image description here

enter image description here

但是,如果我点击单元格中的按钮并转到另一个页面(带有segue);然后使用返回按钮放松,似乎有问题。

enter image description here

所以它看起来像被按下并打开但它不应该有。它应该看起来像顶部图片(只是UIBarButtonItem - 搜索按钮)

enter image description here

我无法弄清楚造成这个问题的问题。

请注意< Back是自动创建的,我没有编写任何代码来创建它。有什么我做错了吗?

更新:添加了一些代码段

首先,创建了一个用于处理搜索的不同类

class SearchBarViewController: UIViewController, UISearchBarDelegate {

  var searchBar : UISearchBar?
  var searchBarWrapper : UIView?
  var searchBarButtonItem : UIBarButtonItem?

  func constructSearchBar()
  {
     searchBarButtonItem = UIBarButtonItem(barButtonSystemItem: .Search, target: self, action: "showSearchBar")
     self.navigationItem.rightBarButtonItem = searchBarButtonItem
  }

  func showSearchBar() {
          // styling & configuration
   }

  func searchBarCancelButtonClicked(searchBar: UISearchBar) {

     UIView.animateWithDuration(0.2, animations: {
         self.searchBar?.resignFirstResponder()
         self.searchBarWrapper?.alpha = 0
         }, completion: { (success) -> Void in
             self.searchBar = nil
             self.searchBarWrapper = nil
             self.navigationItem.rightBarButtonItem = self.searchBarButtonItem
        })
    }
   }

我的ViewController:

 class ViewController: SearchBarViewController {

   override func viewDidLoad() {
      super.viewDidLoad()
      constructSearchBar()
   } 
 }

关于 emrys57 的答案,我尝试在我的ViewController中添加viewWillAppear(),但我无法使其正常工作,因为我的取消看起来有点不同:

override func viewWillAppear(animated: Bool) {
    super.viewDidAppear(animated)

    // Here, I couldn't figure out what to put because
    // my searchBarCancelButtonClicked() needs searchBar and
    // forces me to use (!) but then it says, it's optional..
}

答案是......

 override func viewWillAppear(animated: Bool) {
    super.viewDidAppear(animated)
    navigationItem.titleView = nil
    constructSearchBar()
 }

1 个答案:

答案 0 :(得分:1)

您尚未发布代码,因此并不完全清楚出现了什么问题。使用<ref>,我认为您必须自己单独处理按钮,而不是使用UISearchBar。我想你可能不会在从第二个VC回来时清除搜索栏。此代码清除UISearchController中的搜索栏:

viewWillAppear

当我从按钮推送到第二个VC然后点击class ViewController: UIViewController { var cancelButton: UIBarButtonItem? var searchButton: UIBarButtonItem? override func viewDidLoad() { super.viewDidLoad() cancelButton = UIBarButtonItem(barButtonSystemItem: .Cancel, target: self, action: Selector("searchCancelPressed:")) searchButton = UIBarButtonItem(barButtonSystemItem: .Search, target: self, action: Selector("searchPressed:")) } override func viewWillAppear(animated: Bool) { super.viewDidAppear(animated) searchCancelPressed(nil) } func searchPressed(sender: AnyObject) { navigationItem.titleView = UISearchBar() navigationItem.rightBarButtonItem = cancelButton } func searchCancelPressed(sender: AnyObject?) { navigationItem.titleView = nil navigationItem.rightBarButtonItem = searchButton } } 时,这对我很有用。

在对原始问题进行编辑之后,此代码似乎有效,尽管它可能不是构建答案的最优雅方式:

back

不再需要在override func viewWillAppear(animated: Bool) { super.viewWillAppear(animated) navigationItem.titleView = nil searchBarButtonItem = UIBarButtonItem(barButtonSystemItem: .Search, target: self, action: "showSearchBar") navigationItem.rightBarButtonItem = searchBarButtonItem } 中调用constructSearchBar函数,可以将其删除。