IOS UINavigationBar 自訂返回按鈕

來源:互聯網
上載者:User

很多時候我們使用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

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.