IOS 设置导航栏全局样式

时间:2023-11-25 18:07:56
  1. // 1.设置导航栏背景
  2. UINavigationBar *bar = [UINavigationBar appearance];
  3. [bar setBackgroundImage:[UIImage resizeImage:@"NavigationBar_Background.png"] forBarMetrics:UIBarMetricsDefault];
  4. // 状态栏
  5. [UIApplication sharedApplication].statusBarStyle = UIStatusBarStyleBlackOpaque;
  6. // 2.设置导航栏文字属性
  7. NSMutableDictionary *barAttrs = [NSMutableDictionary dictionary];
  8. [barAttrs setObject:[UIColor darkGrayColor] forKey:UITextAttributeTextColor];
  9. [barAttrs setObject:[NSValue valueWithUIOffset:UIOffsetMake(0, 0)] forKey:UITextAttributeTextShadowOffset];
  10. [bar setTitleTextAttributes:barAttrs];
  11. // 3.按钮
  12. UIBarButtonItem *item = [UIBarButtonItem appearance];
  13. [item setBackgroundImage:[UIImage resizeImage:@"BarButtonItem_Normal.png"] forState:UIControlStateNormal barMetrics:UIBarMetricsDefault];
  14. [item setBackgroundImage:[UIImage resizeImage:@"BarButtonItem_Pressed.png"] forState:UIControlStateHighlighted barMetrics:UIBarMetricsDefault];
  15. NSMutableDictionary *itemAttrs = [NSMutableDictionary dictionaryWithDictionary:barAttrs];
  16. [itemAttrs setObject:[UIFont boldSystemFontOfSize:13] forKey:UITextAttributeFont];
  17. [item setTitleTextAttributes:itemAttrs forState:UIControlStateNormal];
  18. [item setTitleTextAttributes:itemAttrs forState:UIControlStateHighlighted];
  19. [item setTitleTextAttributes:itemAttrs forState:UIControlStateDisabled];
  20. // 4.返回按钮
  21. [item setBackButtonBackgroundImage:[UIImage resizeImage:@"BarButtonItem_Back_Normal.png"] forState:UIControlStateNormal barMetrics:UIBarMetricsDefault];
  22. [item setBackButtonBackgroundImage:[UIImage resizeImage:@"BarButtonItem_Back_Pressed.png"] forState:UIControlStateNormal barMetrics:UIBarMetricsDefault];

搞了半天,居然没有发现可以插入Objective-C代码的地方。真郁闷

  1. //
  2. //  UIImage+Fit.m
  3. //  SinaWeibo
  4. //
  5. //  Created by mj on 13-8-19.
  6. //  Copyright (c) 2013年 itcast. All rights reserved.
  7. //
  8. #import "UIImage+Fit.h"
  9. @implementation UIImage (Fit)
  10. #pragma mark 返回拉伸好的图片
  11. + (UIImage *)resizeImage:(NSString *)imgName {
  12. return [[UIImage imageNamed:imgName] resizeImage];
  13. }
  14. - (UIImage *)resizeImage
  15. {
  16. CGFloat leftCap = self.size.width * 0.5f;
  17. CGFloat topCap = self.size.height * 0.5f;
  18. return [self stretchableImageWithLeftCapWidth:leftCap topCapHeight:topCap];
  19. }
  20. - (UIImage *)cut:(CGSize)sizeScale
  21. {
  22. CGFloat width = self.size.width * sizeScale.width;
  23. CGFloat height = self.size.height * sizeScale.height;
  24. CGFloat x = (self.size.width -  width) * 0.5;
  25. CGFloat y = (self.size.height - height) * 0.5;
  26. CGRect rect = CGRectMake(x, y, width, height);
  27. CGImageRef ref = CGImageCreateWithImageInRect(self.CGImage, rect);
  28. return [UIImage imageWithCGImage:ref];
  29. }
  30. @end