Override back button in navigation stack while keeping appearance of default back button?

后端 未结 8 2075
野的像风
野的像风 2020-12-08 22:38

How do I override the back button for just one view (not for all the back buttons present in different views) such that on click of the back button, root view controller is

相关标签:
8条回答
  • 2020-12-08 23:13

    I had the similar problem and I successfully used the answer given by Sarasranglt. Here is the SWIFT version.

        override func viewDidLoad() {
           let transparentButton = UIButton()
           transparentButton.frame = CGRectMake(0, 0, 50, 40)
           transparentButton.backgroundColor = UIColor.orangeColor()
           transparentButton.addTarget(self, action:"backAction:", forControlEvents:.TouchUpInside)
           self.navigationController?.navigationBar.addSubview(transparentButton)
        }
    

    And the function is

     func backAction(sender:UIButton) {
         // Some sction
     }
    
    0 讨论(0)
  • 2020-12-08 23:14

    Use this code to show a custom back button, Note the backBarButtonItem before marking it as a duplicate answer.

    UIBarButtonItem *backButton = [[UIBarButtonItem alloc] initWithTitle:@"< back"
                                                                   style:UIBarButtonItemStylePlain
                                                                  target:self
                                                                  action:@selector(handleBack:)];
    
    self.navigationItem.backBarButtonItem= backButton;
    

    Cheers!

    0 讨论(0)
提交回复
热议问题