在开发过程中难免会对导航栏进行设置,整理部分导航栏的设置,以备后用
+ (void)initialize { //appearance方法返回一个导航栏的外观对象 //修改了这个外观对象,相当于修改了整个项目中的外观
UINavigationBar *navigationBar = [UINavigationBar appearance];
[navigationBar setBarTintColor:[UIColor colorWithRed:0.91 green:0.12 blue:0.31 alpha:1.0]]; [navigationBar setBackgroundImage:[UIImage imageNamed:@"backimg"] forBarMetrics:UIBarMetricsCompact];
navigationBar.translucent = NO; //去掉导航栏的半透明状态 (不去会有色差)
[[UINavigationBar appearance] setBackgroundImage:[[UIImage alloc] init] forBarPosition:UIBarPositionAny barMetrics:UIBarMetricsDefault];
[[UINavigationBar appearance] setShadowImage:[[UIImage alloc] init]];
[navigationBar setBarStyle:UIBarStyleDefault];//
[navigationBar setBarTintColor:[UIColor redColor]];
[navigationBar setTintColor:[UIColor whiteColor]];
// 设置NavigationBarItem文字的颜色
NSShadow *shadow = [[NSShadow alloc]init];
[shadow setShadowOffset:CGSizeZero];
[navigationBar setTitleTextAttributes:@{ NSForegroundColorAttributeName : [UIColor whiteColor], NSFontAttributeName : [UIFont systemFontOfSize:18], NSShadowAttributeName : shadow }];
//修改所有UIBarButtonItem的外观
UIBarButtonItem *barButtonItem = [UIBarButtonItem appearance];
[barButtonItem setTintColor:[UIColor whiteColor]]; // self.navigationController.interactivePopGestureRecognizer.delegate = (id)self;}
//如果想要统一定制返回按钮样式的话,可以重写如下方法//重写返回按钮
-(void)pushViewController:(UIViewController *)viewController animated:(BOOL)animated{
[super pushViewController:viewController animated:animated];
if (viewController.navigationItem.leftBarButtonItem ==nil && self.viewControllers.count >1) { viewController.navigationItem.leftBarButtonItem = [self creatBackButton]; self.interactivePopGestureRecognizer.delegate = (id)self;
}
}
-(UIBarButtonItem *)creatBackButton
{
return [[UIBarButtonItem alloc]initWithImage: [UIImage imageNamed:@"back"] style:UIBarButtonItemStylePlain target:self action:@selector(popSelf)];
}
-(void)popSelf
{
[self popViewControllerAnimated:YES];
}