How to add back button to UINavigationBar manually?

I want the navigation bar to have a back button that goes to the last page, although there was no navigation bar on the last page. How can I insert a back button and control what happens when a user clicks on it?

+3
source share
4 answers

You need to associate the button from Interface Builder with the method that you want to call when the button is pressed. The method should look like this: - (IBAction)backAction . The method name is up to you, but you must declare a return type in IBAction so that the interface designer knows that a button can be bound to it.

0
source

A navigation bar is not required to control the viewing manager by the navigation controller. Instead of trying to fake the back button, use the navigation controller to control both view controllers and to hide the navigation bar from the first controller. For example, you can add something like:

 [self.navigationController setNavigationBarHidden:YES animated:YES]; 

for the controller method -viewDidAppear. Do something similar for the second controller, passing NO for the hidden parameter to display it again.

In general, the view controller can add a return button to the navigation bar with this code (warning: an unverified code entered from memory, but you need to start):

 UIBarButtonItem *backButton = [[UIBarButtonItem alloc] initWithImage:myBackImage style: UIBarButtonItemStylePlain target:self action:someAction]; self.navigationItem.backBarButtonItem = backButton; [backButton release]; 

When the button is pressed, the button will send its action (someAction) to its target (self).

0
source

I would like to add an answer, but unfortunately I only have the correct answer in Swift. I will provide it here for anyone who comes to this question looking for an answer in Swift. Suppose you want to add a back button to the navigation bar to go to the last page. Do the following:

  let button = UIBarButtonItem.init() button.title = "BACK" let attributes : [NSAttributedStringKey : Any] = [NSAttributedStringKey(rawValue: NSAttributedStringKey.font.rawValue): UIFont.systemFont(ofSize: 15.0)] button.setTitleTextAttributes(attributes, for: .normal) button.target = self button.action = #selector(self.goBack) self.navigationItem.setLeftBarButton(button, animated: false) 

Then define the function (which is used for the selector for the button action above):

 @objc func goBack() { self.performSegue(withIdentifier: "myUnwindSegue", sender: AnyObject.self) } 

Please note that here I use promotion. To use the promotion function, please contact here or leave a comment if you have any questions. Unwinding segments can be tricky when you first use them, but after that they are great. Also note that when you say β€œlast page,” I assume that you are referring to a view already added to the view hierarchy.

0
source
 UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom]; [button addTarget:self action:@selector(back:) forControlEvents:UIControlEventTouchUpInside]; [button.titleLabel setFont:[UIFont boldSystemFontOfSize:11.0]]; [button setBackgroundImage:[UIImage imageNamed:@"back_norm.png"] forState:UIControlStateNormal]; [button setBackgroundImage:[UIImage imageNamed:@"back_click.png"] forState:UIControlStateSelected]; [button setBackgroundImage:[UIImage imageNamed:@"back_click.png"] forState:UIControlStateHighlighted]; [button setTitle:@" Back" forState:UIControlStateNormal]; [button setTitle:@" Back" forState:UIControlStateSelected]; [button setTitle:@" Back" forState:UIControlStateHighlighted]; button.frame = CGRectMake(0, 0, 48, 30); UIBarButtonItem *back = [[UIBarButtonItem alloc] initWithCustomView:button]; self.navigationItem.leftBarButtonItem = back; [button release]; [back release]; 
-2
source

Source: https://habr.com/ru/post/1492337/


All Articles