Hide back button in navigation bar with hidesBackButton in Swift

前端 未结 9 1963
没有蜡笔的小新
没有蜡笔的小新 2021-01-31 08:36

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\"

相关标签:
9条回答
  • 2021-01-31 08:52

    To hide the back button with the latest Swift:

    self.navigationItem.setHidesBackButton(true, animated: false)
    
    0 讨论(0)
  • 2021-01-31 08:55

    this worked for me

    navigationController?.navigationBar.topItem?.hidesBackButton = true
    
    0 讨论(0)
  • 2021-01-31 08:57

    In XCode 11(maybe sooner, not sure), you can also untick the box under the attribute inspector tab in the storyboard editor if you're not looking to do it programatically.

    0 讨论(0)
  • 2021-01-31 09:00

    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
    }
    
    0 讨论(0)
  • 2021-01-31 09:03
    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
    }
    
    0 讨论(0)
  • 2021-01-31 09:05

    Try adding this:

    let backButton = UIBarButtonItem(title: "", style: .Plain, target: navigationController, action: nil)
    navigationItem.leftBarButtonItem = backButton
    
    0 讨论(0)
提交回复
热议问题