ios 更改系统导航条外观

一个类文件第一次使用 会先创建类对象:创建类对象 会调用 +(void)initialize 初始化方法
需要创建实例的时候会通过类对象创建类的对象(实例) : 创建实例会调用 -(id)initxxxxx 初始化方法
//该方法会在第一次使用该类或使用其子类创建实例时,该方法会调用一次
+(void)initialize {
    //修改 系统导航条的外观

    //获取 系统导航控制器中 导航条外观
    UINavigationBar *bar = [UINavigationBar appearance];
    bar.barStyle = UIBarStyleBlack;

    //设置导航条的 背景图片
    [bar setBackgroundImage:[UIImage imageNamed:@" navibg"] forBarMetrics:UIBarMetricsDefault];
    bar.tintColor = [UIColor whiteColor];

    //设置标题的垂直位置
    [bar setTitleVerticalPositionAdjustment:10 forBarMetrics:UIBarMetricsDefault]; 

    //创建属性字符串所需的字典
    NSMutableDictionary *dic = [NSMutableDictionary dictionary];

    //字体
    dic[NSFontAttributeName] = [UIFont boldSystemFontOfSize:20];

    //字体颜色
    dic[NSForegroundColorAttributeName] = [UIColor lightGrayColor];

    //设置导航 title
    [bar setTitleTextAttributes:dic];
}

- (void)viewDidLoad {
    [super viewDidLoad];

    //把tabBar中 的受tintColor的图片取出 用该图片生成一个不受影响图片
    UIImage *selectImage = [self.tabBarItem.selectedImage imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];

    //把不受影响的图片 设置为选中时图片
    self.tabBarItem.selectedImage = selectImage;
}

+(void)initialize {
    //获取 系统 UITabBarItem 的外观
    UITabBarItem *barItem = [UITabBarItem appearance];

    //x 偏移量 title 和 图片都会动  y 偏移量只有 title 会动
//    [barItem setTitlePositionAdjustment:UIOffsetMake(0, 0)];
    
    //设置默认title的颜色 及 字体,创建属性字符串所需的字典
    NSMutableDictionary *dic = [NSMutableDictionary dictionary];
    dic[NSFontAttributeName] = [UIFont systemFontOfSize:11];
    //浮点 0 - 1
    //整型 0 - 255
    //255 103  107 112  ARGB
    //0xffff0512       ARGB
//    dic[NSForegroundColorAttributeName] = [UIColor colorWith255Red:103 green:107 blue:112 alpha:255];
    dic[NSForegroundColorAttributeName] = [UIColor colorWithHex:0xff00ffff];
    [barItem setTitleTextAttributes:dic forState:UIControlStateNormal];
    
    //设置选中时 title 的颜色
    NSMutableDictionary *dic2 = [NSMutableDictionary dictionary];
    dic2[NSFontAttributeName] = [UIFont systemFontOfSize:11];
    dic2[NSForegroundColorAttributeName] = [UIColor colorWith255Red:26 green:178 blue:10 alpha:255];
    [barItem setTitleTextAttributes:dic2 forState:UIControlStateSelected];
    
    //获取 tabBar 的外观
    UITabBar *bar = [UITabBar appearance];

    //设置 背景图片
    [bar setBackgroundImage:[UIImage imageNamed:@" tabbarBkg"]];

    // 设置 bar上被选中的项的背景图片
    //此处用的图片 一定是 9切片处理过的图片
    [bar setSelectionIndicatorImage:[UIImage imageNamed:@"tabbar_selected_back"]];
}
posted @ 2017-08-23 20:54  笑笑就好90  阅读(226)  评论(0编辑  收藏  举报