iOS8设置NavigationBar颜色和BackButtonItem颜色

凤衡
2023-12-01

iOS8设置NavigationBar颜色和BackButtonItem颜色,归档存储以备查阅,把以下代码添加到appdelegate的

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions

//1. 设置导航栏的背景图片
  UINavigationBar *bar=[UINavigationBar appearance];
  //[bar setBackgroundImage:[UIImage imageNamed:@"navigationbar_background.png"] forBarMetrics:UIBarMetricsDefault];
  [bar setBackgroundColor:[UIColor colorWithPatternImage:[UIImage imageNamed:@"navigationbar_background.png"]]];
  bar.barStyle = UIStatusBarStyleDefault;
  //1.2 设置返回按钮的颜色  在plist中添加 View controller-based status bar appearance=NO切记。
  [bar setTintColor:kBackButtonColor];
//  //设置返回按钮指示器图片
//  UIImage *backImage=[[UIImage imageNamed:@"navigationbar_back"] resizableImageWithCapInsets:UIEdgeInsetsMake(5, 0, 5, 5)];
//  [[UINavigationBar appearance] setBackIndicatorImage:backImage];
//  [[UINavigationBar appearance] setBackIndicatorTransitionMaskImage:[UIImage imageNamed:@"navigationbar_back_highlighted"]];
//  
  //2. 设置导航栏文字的主题
  [bar setTitleTextAttributes:@{
                  NSForegroundColorAttributeName:[UIColor blackColor],
                  }];
  //3. 设置UIBarButtonItem的外观
  UIBarButtonItem *barItem=[UIBarButtonItem appearance];
  [barItem setBackgroundImage:[UIImage imageNamed:@"navigationbar_button_background.png"] forState:UIControlStateNormal barMetrics:UIBarMetricsDefault];
  [barItem setBackgroundImage:[UIImage imageNamed:@"navigationbar_button_background_pushed.png"] forState:UIControlStateHighlighted barMetrics:UIBarMetricsDefault];
  //4. 该item上边的文字样式
  NSDictionary *fontDic=@{
              NSForegroundColorAttributeName:kBackButtonColor,
              NSFontAttributeName:[UIFont systemFontOfSize:19.f],  //粗体
              };
  [barItem setTitleTextAttributes:fontDic
               forState:UIControlStateNormal];
  [barItem setTitleTextAttributes:fontDic
               forState:UIControlStateHighlighted];
  // 5.设置状态栏样式
  [UIApplication sharedApplication].statusBarStyle = UIStatusBarStyleLightContent;
 类似资料: