免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 1827 | 回复: 0

[iOS] IOS开发中设置导航栏主题 [复制链接]

论坛徽章:
1
操作系统版块每日发帖之星
日期:2015-06-19 22:20:00
发表于 2015-06-18 13:07 |显示全部楼层
  1. /**
  2. *  系统在第一次使用这个类的时候调用(1个类只会调用一次)
  3. */
  4. + (void)initialize
  5. {
  6.     // 设置导航栏主题
  7.     UINavigationBar *navBar = [UINavigationBar appearance];
  8.     // 设置背景图片
  9.     NSString *bgName = nil;
  10.     if (iOS7) { // 至少是iOS 7.0
  11.         bgName = @"NavBar64";
  12.     } else { // 非iOS7
  13.         bgName = @"NavBar";
  14.     }
  15.     [navBar setBackgroundImage:[UIImage imageNamed:bgName] forBarMetrics:UIBarMetricsDefault];
  16.    
  17.     // 设置标题文字颜色
  18.     NSMutableDictionary *attrs = [NSMutableDictionary dictionary];
  19.     attrs[NSForegroundColorAttributeName] = [UIColor whiteColor];
  20.     attrs[NSFontAttributeName] = [UIFont systemFontOfSize:16];
  21.     [navBar setTitleTextAttributes:attrs];
  22.    
  23.     //设置BarButtonItem的主题
  24.     UIBarButtonItem *item=[UIBarButtonItem appearance];
  25.     //设置文字颜色
  26.     NSMutableDictionary *itemAttrs=[NSMutableDictionary dictionary];
  27.     itemAttrs[NSFontAttributeName]=[UIFont systemFontOfSize:14];
  28.     itemAttrs[NSForegroundColorAttributeName]=[UIColor whiteColor];
  29.     [item setTitleTextAttributes:itemAttrs forState:UIControlStateNormal];
  30.     if (!iOS7) {
  31.         //设置按钮背景
  32.         
  33.         [item setBackgroundImage:[UIImage imageNamed:@"NavButton"] forState:UIControlStateNormal barMetrics:UIBarMetricsDefault];
  34.         [item setBackgroundImage:[UIImage imageNamed:@"NavButtonPressed"] forState:UIControlStateHighlighted barMetrics:UIBarMetricsDefault];
  35.         
  36.         //设置返回按钮的背景
  37.         [item setBackButtonBackgroundImage:[UIImage imageNamed:@"NavButton"] forState:UIControlStateNormal barMetrics:UIBarMetricsDefault];
  38.         [item setBackButtonBackgroundImage:[UIImage imageNamed:@"NavButtonPressed"] forState:UIControlStateHighlighted barMetrics:UIBarMetricsDefault];

  39.     }
  40.     else{
  41.         navBar.tintColor=[UIColor whiteColor];
  42.     }
  43.    }
复制代码
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP