在iOS 7中设置导航栏图像

我想将我目前的项目从iOS 6转换到iOS 7.在iOS 6中,我的项目工作正常,但在iOS 7导航栏中图像显示不正常。

我使用iOS 6的这个代码片段,

UIImage *imgNav = [UIImage imageNamed:@"navigation.png"]; self.navigationController.navigationBar.frame = CGRectMake(0, 0, 320, 44); [self.navigationController.navigationBar setBackgroundImage:imgNav forBarMetrics: UIBarMetricsDefault]; 

如何在iOS 7中设置导航栏图像?

尝试在AppDelegate中添加下面的代码

 [[UINavigationBar appearance] setBackgroundImage:[UIImage imageNamed:@"navigation.png"] forBarMetrics:UIBarMetricsDefault]; 

这是Swift版本:

 UINavigationBar.appearance().setBackgroundImage(UIImage.init(named: "navigation.png"), forBarMetrics: UIBarMetrics.Default) 

对于iOS 7:

 [self.navigationController.navigationBar setBackgroundImage:[UIImage imageNamed:@"navbar.png"] forBarMetrics:UIBarMetricsDefault]; 
 if ([self.navigationController.navigationBar respondsToSelector:@selector(setBackgroundImage:forBarMetrics:)] ) { UIImage *image = [UIImage imageNamed:@"navigation.png"]; [self.navigationController.navigationBar setBackgroundImage:image forBarMetrics:UIBarMetricsDefault]; } 

使用这个简单的语法来改变Navigation Background简单的方法。

 self.navigationController.navigationBar.barTintColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"YourImage.png"]]; self.navigationController.navigationBar.titleTextAttributes = @{NSForegroundColorAttributeName: [UIColor whiteColor]}; 

故事板的方式:

  1. 将图像视图拖到故事板场景的底部栏上。
  2. 按住Control键从场景列表中左侧的导航项拖动到新创build的图像视图。
  3. 点击图像视图,并在属性中设置图像。

[UINavigationBar appearance] setBackgroundImage:[UIImage imageNamed:@“navigation.png”] forBarMetrics:UIBarMetricsDefault];

如果遵循ios7指南中提到的规则,则其工作原理如下:•如果您想要没有渐变的纯色,请创build1 x 1点的图像。 •如果您需要垂直渐变,请创build一个宽度为1点,高度与UI元素背景高度相匹配的图像。 •如果要提供重复纹理外观,则需要创build尺寸与纹理重复部分尺寸相匹配的图像。 •如果要提供不重复的纹理外观,则需要创build尺寸与UI元素的背景区域的尺寸相匹配的静态图像。

欲了解更多信息,请点击以下链接:
https://developer.apple.com/library/ios/documentation/UserExperience/Conceptual/MobileHIG/ResizableImages.html#//apple_ref/doc/uid/TP40006556-CH30-SW1

就这样做

 - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { // This will set the backGround image for all the Navigation Bars [[UINavigationBar appearance] setBackgroundImage:[UIImage imageNamed:@"navigationBar"] forBarMetrics:UIBarMetricsDefault]; return YES; } 
 - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { [[UINavigationBar appearance] setTitleTextAttributes: @{ UITextAttributeTextColor: [UIColor whiteColor], UITextAttributeTextShadowColor: [UIColor clearColor], UITextAttributeTextShadowOffset: [NSValue valueWithUIOffset:UIOffsetMake(0.0f, 1.0f)], UITextAttributeFont: [UIFont fontWithName:@"AppleGothic" size:20.0f] }]; if([[[UIDevice currentDevice] systemVersion] floatValue] < 7.0) { [[UINavigationBar appearance] setBackgroundImage:[UIImage imageNamed:@"navigatio_for_ios6"] forBarMetrics:UIBarMetricsDefault]; [[UINavigationBar appearance] setTitleVerticalPositionAdjustment:0.0 forBarMetrics:UIBarMetricsDefault]; } else { [[UINavigationBar appearance] setBarTintColor:UIColorFromRGB(0x067AB5)]; // Uncomment to change the color of back button [[UINavigationBar appearance] setTintColor:[UIColor whiteColor]]; // Uncomment to assign a custom backgroung image [[UINavigationBar appearance] setBackgroundImage:[UIImage imageNamed:@"navigon_bg_ios7.png"] forBarMetrics:UIBarMetricsDefault]; // Uncomment to change the back indicator image [[UINavigationBar appearance] setBackgroundColor:[UIColor whiteColor]]; [[UINavigationBar appearance] setBackIndicatorTransitionMaskImage:[UIImage imageNamed:@""]]; // Uncomment to change the font style of the title NSShadow *shadow = [[NSShadow alloc] init]; shadow.shadowColor = [UIColor colorWithRed:0.0 green:0.0 blue:0.0 alpha:0.8]; shadow.shadowOffset = CGSizeMake(0, 1); [[UINavigationBar appearance] setTitleTextAttributes: [NSDictionary dictionaryWithObjectsAndKeys:[UIColor colorWithRed:245.0/255.0 green:245.0/255.0 blue:245.0/255.0 alpha:1.0], NSForegroundColorAttributeName,shadow, NSShadowAttributeName,[UIFont fontWithName:@"HelveticaNeue-Bold" size:18.0], NSFontAttributeName, nil]]; [[UINavigationBar appearance] setTitleVerticalPositionAdjustment:0.0 forBarMetrics:UIBarMetricsDefault]; } } 

尝试在AppDelegate类中的代码,它会帮助你。

 [[UINavigationBar appearance] setBackgroundImage:[[UIImage imageNamed:@"navbarimg.png"] resizableImageWithCapInsets:UIEdgeInsetsMake(0, 0, 0, 0)] forBarMetrics:UIBarMetricsDefault];