rightBarButtonItem在iOS-9和iOS-10中弹出时消失

时间:2018-05-14 08:04:47

标签: ios swift uinavigationcontroller uinavigationbar rightbarbuttonitem

我已经阅读了多篇关于此的帖子,但无法使其正常运行。

我已经读过的帖子:

  1. Custom rightBarButtonItem disappearing
  2. RightBarButtonItem disappears when view appears again
  3. 我在ViewController-A中嵌入了UINavigationController。控制器的navigation bar包含rightBarButtonItem。添加rightBarButtonItem的代码:

    let searchButton = UIBarButtonItem(image: UIImage(named: "HomeSearch"), style: .plain, target: self, action: #selector(navigateToSearchScreen))
    self.navigationItem.rightBarButtonItem = searchButton
    

    当另一个ViewController-B被推入导航堆栈并弹回时,rightBarButtonItem将从导航栏中消失。

1 个答案:

答案 0 :(得分:0)

我认为在nextViewcontroller中你隐藏了导航右栏,所以当你弹出然后导航按钮将不会显示所以你必须在viewWillAppear中添加右栏按钮:

override func viewWillAppear(_ animated: Bool) {
        let searchButton = UIBarButtonItem(image: UIImage(named: "HomeSearch"), style: .plain, target: self, action: #selector(navigateToSearchScreen))
        self.navigationItem.rightBarButtonItem = searchButton
    }