NavigationController统一设置自定义导航条
2016-11-08 本文已影响482人
MccReeee
创建个BaseNavigationController继承自UINavigationController
重写push方法
- (void)pushViewController:(UIViewController *)viewController animated:(BOOL)animated{
//设置导航条背景图片
[self.navigationBar setBackgroundImage:[UIImage imageNamed:@"top_bg"] forBarMetrics:UIBarMetricsDefault];
//设置标题颜色
self.navigationBar.tintColor = [UIColor whiteColor];
//通过Attributes设置标题字体颜色字号
[self.navigationBar setTitleTextAttributes:@{@"NSForegroundColorAttributeName":[UIColor whiteColor]}];
NSMutableDictionary *textAttrs = [NSMutableDictionary dictionary];
textAttrs[NSForegroundColorAttributeName] = [UIColor whiteColor];
textAttrs[NSFontAttributeName] = [UIFont boldSystemFontOfSize:18];
[self.navigationBar setTitleTextAttributes:textAttrs];
//如果是根控制器就不显示返回按钮
if (self.childViewControllers.count > 0) { // 非根控制器
UIBarButtonItem *item = [[UIBarButtonItem alloc]initWithImage:[UIImage imageNamed:@"button_back"] style:UIBarButtonItemStylePlain target:self action:@selector(backBtnPressed)];
viewController.navigationItem.leftBarButtonItem = item;
}
[super pushViewController:viewController animated:animated];
}