重写push方法本文中达到的效果就是
self.viewControllers.count == 0,就是第一个页面设置界面(第一个界面和其他的界面不一样,不需要返回等键),下面的控制器可以是一个统一格式,当然,程序执行步骤是先执行push方法,然后才是各个控制器的[View didload],所以当你下面的控制器想要其他的格式的时候可以在[View didload]的里面按需利用自己的东西覆盖
//.h文件
#import <UIKit/UIKit.h>
@interface YLMainNavigationController : UINavigationController
@end
//.m文件
#import "YLMainNavigationController.h"
@implementation YLMainNavigationController
- (void)viewDidLoad {
[super viewDidLoad];
self.navigationBar.barTintColor = [UIColor colorWithRed:1 green:0.8 blue:0.8 alpha:1];
//去除navigationBar下面的黑线
self.navigationBar.barStyle = UIBaselineAdjustmentNone;
}
//导航控制器里面
-(void)pushViewController:(UIViewController *)viewController animated:(BOOL)animated{
if (self.viewControllers.count == 0) {
self.navigationBar.backgroundColor = [UIColor colorWithRed:0.345 green:0.624 blue:0.961 alpha:1];
viewController.navigationItem.leftBarButtonItem = [UIBarButtonItem itemWithTarget:self action:nil image:@"nav_home_logo" highLightImage:nil];;
//扫码
viewController.navigationItem.rightBarButtonItem=[UIBarButtonItem itemWithTarget:self action:@selector(search) image:@"nav_home_sao" highLightImage:@"nav_home_sao"];
}
if (self.viewControllers.count > 0) {
viewController.navigationItem.leftBarButtonItem=[UIBarButtonItem itemWithTarget:self action:@selector(back) image:@"nav_back" highLightImage:@"nav_back"];
viewController.hidesBottomBarWhenPushed=YES;
}
[super pushViewController:viewController animated:YES];
}
-(void)search{
NSLog(@"扫码");
//[self popToRootViewControllerAnimated:YES];//回到根视图
}
-(void)back{
[self popViewControllerAnimated:YES];
}
写一个UIBarButtonItem的Extension
//.h
#import <UIKit/UIKit.h>
@interface UIBarButtonItem (Extension)
+(UIBarButtonItem *)itemWithTarget:(id)target action:(SEL)action image:(NSString *)image highLightImage:(NSString *)highLightImage;
@end
//.m
#import "UIBarButtonItem+extension.h"
@implementation UIBarButtonItem (Extension)
+(UIBarButtonItem *)itemWithTarget:(id)target action:(SEL)action image:(NSString *)image highLightImage:(NSString *)highLightImage
{
UIButton *btn=[[UIButton alloc]init];
[btn addTarget:target action:action forControlEvents:UIControlEventTouchUpInside];
[btn setBackgroundImage:[UIImage imageNamed:image] forState:UIControlStateNormal];
[btn setBackgroundImage:[UIImage imageNamed:highLightImage] forState:UIControlStateHighlighted];
CGSize size=btn.currentBackgroundImage.size;
btn.frame=CGRectMake(0, 0, size.width, size.height);
return [[UIBarButtonItem alloc]initWithCustomView:btn];
}
@end
赶快试试效果吧,自己添加rootViewController,然后看看导航上面的效果,试试就知道妙处
我的主页
沙漠骑士