Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to override self.navigationItem.backBarButtonItem Action?

I want to override self.navigationItem.backBarButtonItem's target and action,

I tried:

UIBarButtonItem *backButton = [[UIBarButtonItem alloc] initWithTitle:@"Back"
                                                                   style:UIBarButtonItemStyleBordered
                                                                  target:self
                                                                  action:@selector(backButtonOverrideAction:)];
[self.navigationItem setLeftBarButtonItem:backButton];

it is working but i want to use the default arrow:

enter image description here

i also tried:

UIBarButtonItem *backButton = self.navigationItem.backBarButtonItem;
[backButton setTarget:self];
[backButton setAction:@selector(backButtonOverrideAction:)];

but unfortunately it's not working.. do you have any idea how to do this?

like image 969
0yeoj Avatar asked May 29 '15 06:05

0yeoj


4 Answers

STILL, No direct access for changing target and action on self.navigationItem.backBarButtonItem.

I ended up using the first one i tried... and get my (custom) image..

UIBarButtonItem *backButton = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"myImage.png"]
                                                               style:UIBarButtonItemStyleBordered
                                                              target:self
                                                              action:@selector(backButtonOverrideAction:)];
[self.navigationItem setLeftBarButtonItem:backButton];

while...

//this two lines of code are still useless, i just feel sorry for them.. 
[self.navigationItem.backBarButtonItem setTarget:<#(id)#>];
[self.navigationItem.backBarButtonItem setAction:<#(SEL)#>];
like image 117
0yeoj Avatar answered Oct 18 '22 10:10

0yeoj


You cannot assign action to back BarButton. The solution for that is to create the custom back button.

like image 22
Dhiraj Avatar answered Oct 18 '22 10:10

Dhiraj


In swift 3.1

override func viewDidLoad() {    
// set navigationItem.leftBarButtonItem return to Root
let backToRootVCButton = UIBarButtonItem.init(title: "Back", style: UIBarButtonItemStyle.plain, target: self, action: #selector(backToRootVCAction))
self.navigationItem.setLeftBarButton(backToRootVCButton, animated: true)

}

func backToRootVCAction() {
    _ = self.navigationController?.popToRootViewController(animated: true)
}
like image 2
Jayce Avatar answered Oct 18 '22 11:10

Jayce


This works for 11.3:

override func viewDidAppear(_ animated: Bool) {
    super.viewDidAppear(animated)
    let BackItemIndex = 2
    if let subViews = navigationController?.navigationBar.subviews, 
        subViews.count > BackItemIndex {
            navigationController?.navigationBar.subviews[BackItemIndex].subviews.first?.addGestureRecognizer(UITapGestureRecognizer(target: self, action: #selector(doPerformAction(_:))))
    }
}

@objc func doPerformAction(_ sender: Any) {
    // some code
}

This solution is based on searching the active subviews until the corresponding to backBarButtonItem and then add a UITapGestureRecognizer, so that it captures the event on the element.

like image 1
Jose Gil Avatar answered Oct 18 '22 11:10

Jose Gil