自定义导航栏上的返回按钮

 

导航栏的按钮,右边的按钮是可以自己随意添加的。但左边的返回按钮怎么定制?

正确的答案是重载UINavigationController类的pushViewController:animated方法。

 1 #import  @interface MyNavigationController: UINavigationController
 2 
 3 { 
 4 
 5 } 
 6 
 7 @end
 8 
 9 #import "MyNavigationController.h" 
10 
11 @implementation MyNavigationController 
12 
13 -(void)popself { 
14 
15 [self popViewControllerAnimated:YES]; 
16 
17 } 
18 
19 -(UIBarButtonItem*) createBackButton 
20 
21 { 
22 
23 return [[UIBarButtonItem alloc] initWithTitle:@"返回" style:UIBarButtonItemStyleBordered target:self action:@selector(popself)]; 
24 
25 } 
26 
27 - (void)pushViewController:(UIViewController *)viewController animated:(BOOL)animated {
28 
29 [super pushViewController:viewControlleranimated:animated];
30 
31 if (viewController.navigationItem.leftBarButtonItem== nil && [self.viewControllers count] > 1) { 
32 
33 viewController.navigationItem.leftBarButtonItem =[self createBackButton];
34 
35 } 
36 
37 }
38 
39 @end 使用MyNavigationController替换UINavigationController。

或者直接创建一个UINavigationController的新类别

posted @ 2012-05-14 11:45  妙笔  阅读(1411)  评论(0编辑  收藏  举报