很多時候我們使用navigationController時由於上一個navigationController.title的字串太長,而導致下一個介面的返回按鈕變的很長也很難看,為了美觀我們我們可以把返回按鈕的title改為back,或者是用自訂的圖片。
1.把返回按鈕的title改為back:這裡要特別說明一下,需要在上一個viewController裡面寫上以下代碼。若是在當前viewController中無論你是用self.navigationItem.leftBarButtonItem.title
= @"back"; 還是用self.navigationItem.backBarButtonItem.title = @"back"; 都不會修改返回按鈕的title。代碼如下:
- (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]) {// 下一個介面的返回按鈕UIBarButtonItem *temporaryBarButtonItem = [[UIBarButtonItem alloc] init];temporaryBarButtonItem.title = @"back";temporaryBarButtonItem.target = self;temporaryBarButtonItem.action = @selector(back:);self.navigationItem.backBarButtonItem = temporaryBarButtonItem;[temporaryBarButtonItem release];}return self;}
.用自訂的圖片修改返回按鈕:
-(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;}
原文地址:http://blog.csdn.net/jinglijun/article/details/7001356