很多时候我们使用navigationController时由于上一个navigationController.title的字符串太长,而导致下一个界面的返回按钮变的很长也很难看,为了美观我们我们可以把返回按钮的title改为back,或者是用自定义的图片。
1.把返回按钮的title改为back:这里要特别说明一下,需要在上一个viewController里面写上以下代码。若是在当前viewController中无论你是用self.navigationItem.leftBarButtonItem.title = @"back"; 还是用self.navigationItem.backBarButtonItem.title = @"back"; 都不会修改返回按钮的title。代码如下:
Objective-c代码
- - (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;
- }
2.用自定义的图片修改返回按钮:
Objective-c代码
- -(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;
- }