【问题标题】:hidesBackButton is not working, even though in viewDidLoadhidesBackButton 不起作用,即使在 viewDidLoad
【发布时间】:2020-11-11 05:01:46
【问题描述】:

我见过类似的问题,但这些问题都是非常古老的。我试图在我的 ViewController 中隐藏“后退”按钮。现在,它看起来像这样:https://www.dropbox.com/s/ipmbqgxji574j06/Screenshot%202020-07-21%2020.25.17.png?dl=0

我的代码 (我已经尝试在 viewWillAppear 和 viewDidLoad 中实现它,似乎都不起作用)

override func viewWillAppear(_ animated: Bool) {
    super.viewWillAppear(animated)
    
    // Setting up title of ViewController
    self.navigationController?.isNavigationBarHidden = false
    self.navigationController?.navigationBar.topItem?.title = "Groceries"
    self.navigationController?.navigationBar.prefersLargeTitles = true
    self.navigationItem.hidesBackButton = true
}

override func viewDidLoad() {
    super.viewDidLoad()
    self.navigationItem.setHidesBackButton(true, animated: true)
    print(self.navigationItem.hidesBackButton)
    // Returns true
}

我也尝试将代码放入 prepareForSegue:

override func prepare(for segue: UIStoryboardSegue, sender: Any?){
    if segue.identifier == K.welcomeSegue{
    let objVC = segue.destination as? TableViewController
    objVC?.navigationItem.hidesBackButton = true
  }
}

谁能帮帮我?

谢谢!

【问题讨论】:

    标签: ios swift uinavigationcontroller uinavigationitem navigationcontroller


    【解决方案1】:

    要隐藏navigationBar 上的backButton,请将navigationButton 设置为nil,然后将其隐藏或直接隐藏。

    即使您设置了自定义navigationBar,将这些方法结合起来将是一个更好的解决方案并且有效。

    self.navigationItem.leftBarButtonItem = nil
    self.navigationItem.hidesBackButton = true
    

    你也可以使用

    override func viewDidLoad() {
       super.viewDidLoad()
       self.navigationItem.setHidesBackButton(true, animated: false)
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-07-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多