锦集05-导航栏的那些事

导航栏的统一样式设置

  1. 凡是某个类声明的方法中有出现UI_APPEARANCE_SELECTOR这个字样的,说明:
    1. 该类可以通过[Class appearance]创建
    2. 该类可以通过上面的创建方式,统一设置整个app的样式
  2. 统一设置:
    1. 创建

       UINavigationBar *bar = [UINavigationBar appearance];
      
    2. 背景色

       //背景颜色
       bar.barTintColor = kRGBColorFromHex(00D8D5);
      
    3. 背景图片

       [bar setBackgroundImage:[UIImage imageNamed:@"navigationbarBackgroundWhite"] forBarMetrics:UIBarMetricsDefault];
      
    4. title样式

       NSMutableDictionary *attr = [NSMutableDictionary dictionary];
       attr[NSFontAttributeName] = [UIFont boldSystemFontOfSize:18.f];
       attr[NSForegroundColorAttributeName] = [UIColor whiteColor];
       [bar setTitleTextAttributes:attr];
      
      1. 主要是设置导航栏中间title的文字和颜色
      2. 同时也能把当前app所有的rightBarButtonItem、leftBarButtonItem、backBarButtonItem渲染成统一颜色
      3. 但是: 调用系统的控制器的时候(比如通讯录、相册等),rightBarButtonItem、leftBarButtonItem、backBarButtonItem不会被渲染
      4. 该方法不能统一设置,rightBarButtonItem、leftBarButtonItem、backBarButtonItem的字体大小,即这些字体大小不会改变
    5. item样式

       [bar setTintColor:[UIColor whiteColor]];
      
      1. 用于设置当前app所有的rightBarButtonItem、leftBarButtonItem、backBarButtonItem渲染成统一颜色(不管是否是系统的控制器,同样适用)
      2. 不能设置title的颜色
      3. 这写字体大小,不能统一设置需要通过self.navigationController.navigationItem...,独立设置
    6. 去掉导航栏上的自带渲染
      1. 当想让导航栏的颜色跟控制器View的颜色一样时,会发现颜色颜色不一样,尽管设置的RGB一样,由于ios7 以上导航栏默认透明导致的,可以去掉导航栏透明度

         //去掉渲染透明
         bar.translucent = NO;
        
    7. 去掉导航栏的线
      1. 是由于苹果默认有shadowImage 导致

         [bar setShadowImage:[[UIImage alloc] init]];
        

TabBar 的统一设置

  1. 统一设置tabbar
    1. 创建:

       UITabBar *tabbar =  [UITabBar appearance];
      
    2. 去掉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];
      
    3. 设置tabar背景色

       tabbar.backgroundColor = [UIColor whiteColor];
      
    4. 待补充…

  2. 统一设置item样式
    1. 创建

       UITabBarItem *item = [UITabBarItem appearance];
      
    2. 设置文字选中/未选中的样式

       //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];
      
Table of Contents