前言:
搭建APP的时候少不了设置UITabBarController,UINavigationController;
UITabBarController是为了管理控制器,而UINavigationController是为了统一布局;
本篇介绍导航控制器,主要涉及到以下“控件”的设置
一、UINavigationBar
1、导航控制器的整个背景颜色
self.navigationBar.barTintColor = [UIColor redColor];//设置为红色
2、设置文字颜色和大小
// 设置UINavigationBar文字属性
UINavigationBar *appearance = [UINavigationBar appearance];
NSMutableDictionary *textAttrs = [NSMutableDictionary dictionary];
textAttrs[NSFontAttributeName] = [UIFont systemFontOfSize:18];
textAttrs[NSForegroundColorAttributeName] = [UIColor whiteColor];
[appearance setTitleTextAttributes:textAttrs];
二、UIBarButtonItem
这里分一般和自定义进行设置:
1、一般情况:(与设置UINavigationBar一样)
UIBarButtonItem *appearance = [UIBarButtonItem appearance];
NSMutableDictionary *textAttrs = [NSMutableDictionary dictionary];
textAttrs[NSForegroundColorAttributeName] = [UIColor whiteColor];
[appearance setTitleTextAttributes:textAttrs forState:UIControlStateNormal];
2、自定义:重写导航控制器的 pushViewController 方法
-(void)pushViewController:(UIViewController *)viewController animated:(BOOL)animated{
if (self.viewControllers.count > 0) {
/** 推出控制器之后隐藏tabBar */
viewController.hidesBottomBarWhenPushed = YES;
viewController.navigationItem.leftBarButtonItem = [UIBarButtonItem barButtonItemWithBg:@"icon_return" title:@"返回" target:self action:@selector(back)];
**这里对BarButtonItem进行自定义**
}
[super pushViewController:viewController animated:YES];
}
-(void)back
{
[self popViewControllerAnimated:YES];
}
对BarButtonItem进行扩展
+ (UIBarButtonItem *)barButtonItemWithBg:(NSString *)bg title:(NSString *)title target:(id)target action:(SEL)action
{
UIButton *button = [[UIButton alloc]init];
[button setImage:[UIImage imageNamed:bg] forState:UIControlStateNormal];
[button setImageEdgeInsets:UIEdgeInsetsMake(0, -30, 0, 0)];
[button setTitle:title forState:UIControlStateNormal];
[button setTitleEdgeInsets:UIEdgeInsetsMake(0, -55, 0, 0)];
[button setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
button.titleLabel.font = [UIFont systemFontOfSize:14];
button.frame = CGRectMake(0, 0, 60, 44);
[button addTarget:target action:action forControlEvents:UIControlEventTouchUpInside];
return [[UIBarButtonItem alloc] initWithCustomView:button];
}