Hide back button in navigation bar with hidesBackB

2019-03-09 01:16发布

I want to hide the back button when transitioning from one view to another. I read the questions regarding this problem and every answer was "use hidesBackButton". The problem with this is:

  • when I put it in viewDidLoad/viewWillAppear the back button arrow hides but the string "Back" doesn't.

  • when I put it in viewDidAppear the back button disappears but it visible to the user

How can I fix this?

Edit:

Here is how you can replicate this problem(or bug?)
Make a new Tabbed application with Swift in Xcode. In the FirstViewController.swift use performSegueWithIdentifier to navigate to the second view controller. In the SecondViewController.swift hide the navigation bar back button using hidesBackButton and you will see what the problem is.

8条回答
叛逆
2楼-- · 2019-03-09 01:48

This worked for me:

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

    self.tabBarController?.navigationItem.hidesBackButton = true
}
查看更多
家丑人穷心不美
3楼-- · 2019-03-09 01:52
override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view.

    self.navigationController?.navigationBarHidden = false
    var button: UIButton = UIButton()
    button.setImage(UIImage(named: "person-icon.jpg"), forState: .Normal)
    button.frame = CGRectMake(0, 0, 25, 25)
    button.targetForAction("actioncall", withSender: nil)
    var rightItem:UIBarButtonItem = UIBarButtonItem()
    rightItem.customView = button
    self.navigationItem.rightBarButtonItem = rightItem

    let backButton = UIBarButtonItem(title: "", style: UIBarButtonItemStyle.Plain, target: navigationController, action: nil)
    navigationItem.leftBarButtonItem = backButton
}

override func viewWillAppear(animated: Bool) {
    let backButton = UIBarButtonItem(title: "", style: UIBarButtonItemStyle.Plain, target: navigationController, action: nil)
    navigationItem.leftBarButtonItem = backButton
}
查看更多
我欲成王,谁敢阻挡
4楼-- · 2019-03-09 01:58

Try adding this:

let backButton = UIBarButtonItem(title: "", style: .Plain, target: navigationController, action: nil)
navigationItem.leftBarButtonItem = backButton
查看更多
Juvenile、少年°
5楼-- · 2019-03-09 02:04

Worked for me when I set it in init(), instead of viewDidLoad. Strange though

查看更多
Evening l夕情丶
6楼-- · 2019-03-09 02:07

Try adding this,This worked for me

navigationItem.hidesBackButton = true
查看更多
Rolldiameter
7楼-- · 2019-03-09 02:09

You can use the code below to hide back button on UINavigationBar.

Swift 3;

override func viewWillAppear(_ animated: Bool) {
    super.viewWillAppear(animated)
    self.navigationItem.hidesBackButton = true
}
查看更多
登录 后发表回答