The back navigation button does not show how to add a back button to UINavigationItem

I am adding a UINavigationBar through the library for viewing. I also add a UINavigationItem to this NavigationBar.

In viewController.m I added the following code, but the back button is not displayed.

self.navigationItem.title = @"List"; self.navigationItem.backBarButtonItem.title = @"Back"; 
+6
source share
5 answers

Try the following:

 self.navigationItem.leftItemsSupplementBackButton = YES; 

If you configure your navigation controller using UIBarButtonItems, the framework removes the default back button because it assumes that you are customizing the navigation bar. Use this line to add the back button again.

+8
source

self.navigationItem.backBarButtonItem will only be displayed after you push another view self.navigationController navigation stack, controlled by self.navigationController , if the left button on the navigation bar is not displayed.

From Apple docs:

When this element is the back element of the navigation bar, when it is the next element below the top element, it can be represented as the "Back" button on the navigation bar. Use this property to specify the back button. The purpose and action of the back panel button element that you set must be zero. The default value is a panel item with a title for the navigation items.

+2
source

Try this code,

 **To hide your default back button use this,** self.navigationItem.hidesBackButton = YES; UIImage* image = [UIImage imageNamed:@"back.png"]; CGRect frame = CGRectMake(0, 0, image.size.width, image.size.height); UIButton* backbtn = [[UIButton alloc] initWithFrame:frame]; [backbtn setBackgroundImage:image forState:UIControlStateNormal]; [backbtn setShowsTouchWhenHighlighted:YES]; [backbtn addTarget:self action:@selector(goBack) forControlEvents:UIControlEventTouchUpInside]; UIBarButtonItem* backButtonItem = [[UIBarButtonItem alloc] initWithCustomView:backbtn]; [self.navigationItem setLeftBarButtonItem:backButtonItem]; [backButtonItem release]; [backbtn release]; 

Action for the back button:

 -(IBAction)goBack{ //ur code here 

}

+2
source

Try this code, maybe this will help u

 UIButton *moreButton1 = [UIButton buttonWithType:UIButtonTypeCustom]; [moreButton1 setImage:[UIImage imageNamed:@"left_arrow.png"] forState:UIControlStateNormal]; //[moreButton setImage:[UIImage imageNamed:@"Button_OtherInfo_Active.png"] forState:UIControlStateHighlighted]; [moreButton1 addTarget:self action:@selector(backClick) forControlEvents:UIControlEventTouchUpInside]; [moreButton1 setFrame:CGRectMake(0, 0, 50,30)]; self.navigationItem.leftBarButtonItem=[[UIBarButtonItem alloc] initWithCustomView:moreButton1]; 

enter this code in viewDidLoad

 -(void)backClick { [self.navigationController popViewControllerAnimated:YES]; } 
+2
source

This is a bit outdated, but if someone is looking for an answer ...

The back button is displayed automatically, but the current view (and not the one that is pressed) should have a title. eg:

 - (void)viewDidLoad { self.title = @"Home"; [super viewDidLoad]; } 

Then when you click the view on the view stack with

 [self.navigationController pushViewController:aViewController animated:YES]; 

the back button will appear. No need to contact UINavigationItem or UINavigationBar. Use them to customize the navigation bar. Take a look at an example project called NavBar, part of xcode.

0
source

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


All Articles