锦集05-导航栏的那些事
导航栏的统一样式设置
- 凡是某个类声明的方法中有出现UI_APPEARANCE_SELECTOR这个字样的,说明:
- 该类可以通过
[Class appearance]
创建 - 该类可以通过上面的创建方式,统一设置整个app的样式
- 该类可以通过
- 统一设置:
-
创建
UINavigationBar *bar = [UINavigationBar appearance];
-
背景色
//背景颜色 bar.barTintColor = kRGBColorFromHex(00D8D5);
-
背景图片
[bar setBackgroundImage:[UIImage imageNamed:@"navigationbarBackgroundWhite"] forBarMetrics:UIBarMetricsDefault];
-
title样式
NSMutableDictionary *attr = [NSMutableDictionary dictionary]; attr[NSFontAttributeName] = [UIFont boldSystemFontOfSize:18.f]; attr[NSForegroundColorAttributeName] = [UIColor whiteColor]; [bar setTitleTextAttributes:attr];
- 主要是设置导航栏中间title的文字和颜色
- 同时也能把当前app所有的rightBarButtonItem、leftBarButtonItem、backBarButtonItem渲染成统一颜色
- 但是: 调用系统的控制器的时候(比如通讯录、相册等),rightBarButtonItem、leftBarButtonItem、backBarButtonItem不会被渲染
- 该方法不能统一设置,rightBarButtonItem、leftBarButtonItem、backBarButtonItem的字体大小,即这些字体大小不会改变
-
item样式
[bar setTintColor:[UIColor whiteColor]];
- 只用于设置当前app所有的rightBarButtonItem、leftBarButtonItem、backBarButtonItem渲染成统一颜色(不管是否是系统的控制器,同样适用)
- 不能设置title的颜色
- 这写字体大小,不能统一设置需要通过
self.navigationController.navigationItem...
,独立设置
- 去掉导航栏上的自带渲染
-
当想让导航栏的颜色跟控制器View的颜色一样时,会发现颜色颜色不一样,尽管设置的RGB一样,由于ios7 以上导航栏默认透明导致的,可以去掉导航栏透明度
//去掉渲染透明 bar.translucent = NO;
-
- 去掉导航栏的线
-
是由于苹果默认有shadowImage 导致
[bar setShadowImage:[[UIImage alloc] init]];
-
-
TabBar 的统一设置
- 统一设置tabbar
-
创建:
UITabBar *tabbar = [UITabBar appearance];
-
去掉tabBar顶部线条
CGRect rect = CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT); UIGraphicsBeginImageContext(rect.size); CGContextRef context = UIGraphicsGetCurrentContext(); CGContextSetFillColorWithColor(context, [[UIColor clearColor] CGColor]); CGContextFillRect(context, rect); UIImage *img = UIGraphicsGetImageFromCurrentImageContext(); UIGraphicsEndImageContext(); [tabbar setBackgroundImage:img]; [tabbar setShadowImage:img];
-
设置tabar背景色
tabbar.backgroundColor = [UIColor whiteColor];
-
待补充…
-
- 统一设置item样式
-
创建
UITabBarItem *item = [UITabBarItem appearance];
-
设置文字选中/未选中的样式
//1.设置文字没被选中的样式 NSMutableDictionary *atrri = [NSMutableDictionary dictionary]; atrri[NSFontAttributeName] = [UIFont systemFontOfSize:12]; atrri[NSForegroundColorAttributeName] = UIColorFromRGB(182, 182, 182); //2.设置文字被选中的样式 NSMutableDictionary *selectedArri = [NSMutableDictionary dictionary]; selectedArri[NSFontAttributeName] = [UIFont systemFontOfSize:12]; selectedArri[NSForegroundColorAttributeName] = kRGBColorFromHex(00D8D5); //3.统一设置 [item setTitleTextAttributes:atrri forState:UIControlStateNormal]; [item setTitleTextAttributes:selectedArri forState:UIControlStateSelected];
-