Custom button on the navigation bar

There are many UIViewControllers in my application with UINavigationControllers . There should be a back button and a home UIButton on the UINavigationBar . It all works great.

But some of my UIViewControllers have long names, and sometimes there is too little space for it. I am trying to replace the original label of the Back button (it shows the name of the previous view) with the custom Back, but everything I tried does not work:

 // Title didn't change [self.navigationItem.backBarButtonItem setTitle:@"Back"]; // Action didn't set, no response from button ( button didn't do anything ) [self.navigationItem.leftBarButtonItem setAction:self.navigationItem.backBarButtonItem.action]; 

And I need a back button to have a style like in this question: Set up a custom back button on the iPhone navigation bar

+8
ios iphone uinavigationitem uinavigationcontroller
source share
6 answers

try it

 UIBarButtonItem *backBarBtnItem = [[UIBarButtonItem alloc] initWithTitle:@"Back" style:UIBarButtonItemStylePlain target:self action:@selector(popViewController)]; [self.navigationItem setBackBarButtonItem:backBarBtnItem]; - (void)popViewController { [self.navigationController popViewControllerAnimated:YES]; } 
+6
source share

Try the following. This will definitely work:

 - (void)viewDidLoad { [super viewDidLoad]; UIImage *buttonImage = [UIImage imageNamed:@"back.png"]; UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom]; [button setImage:buttonImage forState:UIControlStateNormal]; button.frame = CGRectMake(0, 0, buttonImage.size.width, buttonImage.size.height); [button addTarget:self action:@selector(back) forControlEvents:UIControlEventTouchUpInside]; UIBarButtonItem *customBarItem = [[UIBarButtonItem alloc] initWithCustomView:button]; self.navigationItem.leftBarButtonItem = customBarItem; [customBarItem release]; } - (void)back { [self.navigationController popViewControllerAnimated:YES]; } 

Make sure that you have a button image with the size of the button for navigating to the navigation bar in the resource folder named back.png .

Feel free if any other help is needed.

+50
source share

Purpose: setting the entire back button on the UINavigationBar to a white icon

Steps: 1. in the method "didFinishLaunchingWithOptions" AppDelete:

UIImage * backBtnIcon = [UIImage imageNamed: @ "navBackBtn"];

 if (SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(@"7.0")) { [UINavigationBar appearance].tintColor = [UIColor whiteColor]; [UINavigationBar appearance].backIndicatorImage = backBtnIcon; [UINavigationBar appearance].backIndicatorTransitionMaskImage = backBtnIcon; }else{ UIImage *backButtonImage = [backBtnIcon resizableImageWithCapInsets:UIEdgeInsetsMake(0, backBtnIcon.size.width - 1, 0, 0)]; [[UIBarButtonItem appearance] setBackButtonBackgroundImage:backButtonImage forState:UIControlStateNormal barMetrics:UIBarMetricsDefault]; [[UIBarButtonItem appearance] setBackButtonTitlePositionAdjustment:UIOffsetMake(0, -backButtonImage.size.height*2) forBarMetrics:UIBarMetricsDefault]; } 

2. in the viewDidLoad method of the regular SuperController class:

  if (SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(@"7.0")) { UIBarButtonItem *backItem = [[UIBarButtonItem alloc] initWithTitle:@"" style:UIBarButtonItemStylePlain target:nil action:nil]; [self.navigationItem setBackBarButtonItem:backItem]; }else{ //do nothing } 
+7
source share

If you do this everywhere, like me, you'd better implement Anil's solution as a category:

 @interface UIViewController (CustomBackButton) - (void) setCustomBackButton; - (void) back; @end @implementation UIViewController (CustomBackButton) - (void) setCustomBackButton { UIImage *buttonImage = [UIImage imageNamed:@"back.png"]; UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom]; [button setImage:buttonImage forState:UIControlStateNormal]; button.frame = CGRectMake(0, 0, buttonImage.size.width, buttonImage.size.height); [button addTarget:self action:@selector(back) forControlEvents:UIControlEventTouchUpInside]; UIBarButtonItem *customBarItem = [[UIBarButtonItem alloc] initWithCustomView:button]; self.navigationItem.leftBarButtonItem = customBarItem; } - (void) back { [self.navigationController popViewControllerAnimated:YES]; } @end 
+2
source share

Simply put:

 UIBarButtonItem *barBtnItem = [[UIBarButtonItem alloc]initWithTitle:@"Indietro" style:UIBarButtonItemStyleBordered target:self action:@selector(pop)]; [barBtnItem setTintColor:[UIColor whiteColor]]; self.navigationItem.leftBarButtonItem = barBtnItem; 
+1
source share

Suppose you have two controllers - Controller1 and Controller2. Controller2 pops out of Controller1. Therefore, before clicking Controller2 on the navigationController from Controller1

 Controller2 *controller2 = [[[Controller2 alloc] init]autorelease]; self.navigationItem.hidesBackButton = YES; 

Now in the viewDidLoad: Controller2 method add the following snippet

 UIBarButtonItem *backBarButtonItem =[[[UIBarButtonItem alloc]initWithTitle:@"Back" style:UIBarButtonItemStyleBordered target:self action:@selector(goBackToAllPets:)]autorelease]; self.navigationItem.leftBarButtonItem = backBarButtonItem; 

and in the backButtonClicked method you can perform the checks you want.

0
source share

All Articles