IOS uinavigationbar custom return button

Source: Internet
Author: User

Most of the time we use navigationcontroller because of the previous navigationcontroller. the title string is too long, which makes the return button of the next interface very long and ugly. To make it more beautiful, we can change the title of the return button to back or use a custom image.

1. Change the title of the return button to back: here we need to note that the following code should be written in the previous viewcontroller. If you use self. navigationitem. leftbarbuttonitem. title in the current viewcontroller
= @ "Back"; or use self. navigationitem. backbarbuttonitem. Title = @ "back"; the title of the return button is not modified. The Code is as follows:

-(Void) Back :( ID) sender {[self. navigationcontroller popviewcontrolleranimated: Yes];} // The designated initializer. override if you create the Controller programmatically and want to perform customization that is not appropriate for viewdidload. -(ID) initwithnibname :( nsstring *) nibnameornil bundle :( nsbundle *) nibbundleornil {If (Self = [Super initwithnibname: nibnameornil Bundle: nibbundleornil]) {// the return button of the next interface uibarbuttonitem * temporarybarbuttonitem = [[uibarbuttonitem alloc] init]; temporarybarbuttonitem. title = @ "back" using temporarybarbuttonitem.tar get = self; temporarybarbuttonitem. action = @ selector (Back :); self. navigationitem. backbarbuttonitem = temporarybarbuttonitem; [temporarybarbuttonitem release];} return self ;}

. Use the custom image modification return button:

-(IBAction)backAction{ [self.navigationController popViewControllerAnimated:YES];} // The designated initializer. Override if you create the controller programmatically and want to perform customization that is not appropriate for viewDidLoad.- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil { if (self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]) { // Custom initialization UIButton *backButton = [UIButton buttonWithType:UIButtonTypeCustom]; backButton.frame = CGRectMake(0.0, 0.0, 40.0, 27.0); [backButton setImage:[UIImage imageNamed:@"Btn_Back_Off.png"] forState:UIControlStateNormal]; [backButton setImage:[UIImage imageNamed:@"Btn_Back_On.png"] forState:UIControlStateSelected]; [backButton addTarget:self action:@selector(backAction) forControlEvents:UIControlEventTouchUpInside]; UIBarButtonItem *temporaryBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:backButton]; temporaryBarButtonItem.style = UIBarButtonItemStylePlain; self.navigationItem.leftBarButtonItem=temporaryBarButtonItem;[temporaryBarButtonItem release];}return self;}

Address: http://blog.csdn.net/jinglijun/article/details/7001356

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.