导航栏的按钮,右边的按钮是可以自己随意添加的。但左边的返回按钮怎么定制?你会说,添加一个自己的按钮呗!你可以试试看,这样行不行。
正确的答案是重载UINavigationController类的pushViewController:animated方法。
01 |
#import <UIKit/UIKit.h> |
03 |
<a href= "http://my.oschina.net/interface" class = "referer" target= "_blank" >@interface</a> MyNavigationController: UINavigationController { |
07 |
<a href= "http://my.oschina.net/end" class = "referer" target= "_blank" >@end</a> |
10 |
#import "MyNavigationController.h" |
12 |
@implementation MyNavigationController |
18 |
[self popViewControllerAnimated:YES]; |
22 |
-(UIBarButtonItem*) createBackButton |
26 |
return [[UIBarButtonItem alloc] |
30 |
style:UIBarButtonItemStyleBordered |
34 |
action:@selector(popself)]; |
38 |
- ( void )pushViewController:(UIViewController *)viewController animated:( BOOL )animated |
42 |
[super pushViewController:viewControlleranimated:animated]; |
44 |
if (viewController.navigationItem.leftBarButtonItem== nil && [self.viewControllers count] > 1) { |
46 |
viewController.navigationItem.leftBarButtonItem =[self createBackButton]; |
使用MyNavigationController替换UINavigationController。或者直接创建一个UINavigationController的新类别——不过,这招太毒了。会影响到所有的导航控制器。做人还是留一线的好。