1. 自定义NavigationController:
@interface CustomNavigationController : UINavigationController
2. 重写Push方法, 拦截Push进来的控制器:
- (void)pushViewController:(UIViewController *)viewController animated:(BOOL)animated { // 判断Push进来的控制器是否根控制器 if (self.childViewControllers.count > 0) { // Push时隐藏TabBarBottom(应用层级结构为TabBarController -> NavigationController时) viewController.hidesBottomBarWhenPushed = YES; // 添加左边Item到navigationItem viewController.navigationItem.leftBarButtonItem = [UIBarButtonItem itemWithTarget:self image:@“leftBarButtonImage" highlightedImage:@"leftBarButtonImage_highlighted" action:@selector(clickLeftItem)]; // 添加左边Item到navigationItem viewController.navigationItem.rightBarButtonItem = [UIBarButtonItem itemWithTarget:self image:@“rightBarButtonImage" highlightedImage:@“rightBarButtonImage_highlighted" action:@selector(clickRightItem)]; } [super pushViewController:viewController animated:YES]; }
Item点击事件:
- (void)clickLeftItem { // Method [self popViewControllerAnimated:YES]; }
- (void)clickLeftItem { // Method }
3. 通过Category,自定义BarButtonItem创建方法:
+ (UIBarButtonItem *)itemWithTarget:(id)target image:(NSString *)imageName highlightedImage:(NSString *)highlightedImageName action:(SEL)action { // 创建按钮 UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom]; // 设置按钮图片 [button setImage:[UIImage imageNamed:imageName] forState:UIControlStateNormal]; [button setImage:[UIImage imageNamed:highlightedImageName] forState:UIControlStateHighlighted]; // 设置按钮尺寸 button.size = button.currentImage.size; // 避免图片拉伸 // 设置按钮监听事件 [button addTarget:target action:action forControlEvents:UIControlEventTouchUpInside]; // 返回Item return [[UIBarButtonItem alloc] initWithCustomView:button]; }