iOS 7导航栏背景图像的问题

我正在使用图像作为导航栏背景图片。 设置图片我使用下面的代码:

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

对于iOS7“nav_logo_ios7.png”图片大小为768×64,对于iOS6和波纹pipe,我使用的图片大小为768×44。

这在所有的UIViewControllers上都能正常工作。

在这里输入图像说明

在同一个项目中我使用UIActivityViewController 。 在iOS7邮件撰写视图看起来像这样:

在这里输入图像说明

我该如何处理?

提前致谢。

你面对的问题是,当一个UIViewController模态地呈现,状态栏不包括在UINavigationBar的高度。

这意味着64pt图像不正确。

首先, 官方和更好的方法来检查设备正在运行的iOS版本是做这样的事情:

 if (NSFoundationVersionNumber > NSFoundationVersionNumber_iOS_6_1) { //handle iOS 7 Stuff } else { //handle older iOS versions } 

有关更多信息,请查看NSObjCRuntime.h标头。

UINavigationBar背景图像不应该是一个固定大小的图像,而应该是可伸缩的图像,如可重复的图案,所以也许这将是一个想法,重新考虑未来的devise…但是,如果你想继续自定义的大小的图像那我给你一个build议

UINavigationController允许你使用initWithNavigationBarClass:toolbarClass:定义UINavigationBar和UIToolbar类的实例initWithNavigationBarClass:toolbarClass: …这意味着你可以初始化任何视图,而不是以模态方式用不同的UINavigationBar子类呈现给模态表示的视图。

这意味着您将能够指定不同的背景图像,取决于您的导航控制器是否呈现模式,例如:

 UIImage *backgroundImage44pts = [UIImage imageNamed:@" ... "]; UIImage *backgroundImage64pts = [UIImage imageNamed:@" ... "]; if (NSFoundationVersionNumber > NSFoundationVersionNumber_iOS_6_1) { //handle iOS 7 Stuff [[UINavigationBar appearance] setBackgroundImage:backgroundImage44pts forBarMetrics:UIBarMetricsDefault]; [[UINavigationBarSubclass appearance] setBackgroundImage:backgroundImage64pts forBarMetrics:UIBarMetricsDefault]; } else { //handle older iOS versions [[UINavigationBar appearance] setBackgroundImage:backgroundImage44pts forBarMetrics:UIBarMetricsDefault]; } 

一个重要的事情要注意的是,MFMailComposeViewController不是一个真正的视图控制器,所以试图用自定义导航栏子类初始化可能无法正常工作..这就是为什么我已经使用所有非模态导航控制器的自定义导航栏子类和而不是相反。

另外要注意的是,如果你的应用程序是通用的,那么模态视图就不存在了(除非你有任何自定义),你不必担心这一点。

正如我前面所说… UINavigationBars并不是真的有固定大小的背景图像(这就是为什么它很难实现),所以如果你认为这个解决方法太复杂了,那么反思一下也许是个好主意你的devise。

还有最后一件事(我承诺)… iOS 7中的主要devise变更之一是让您的内容从导航栏中stream动到状态栏下方。添加图像以防止出现这种情况,并将其replace为纯白色背景对于iOS 7应用程序来说似乎相当奇怪。

 //In `AppDelegate.m` - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { if([[[UIDevice currentDevice] systemVersion] floatValue] < 7.0) { [[UINavigationBar appearance] setBackgroundImage:[UIImage imageNamed:@"nav_bg.png"] forBarMetrics:UIBarMetricsDefault]; [[UINavigationBar appearance] setTitleTextAttributes: @{ UITextAttributeTextColor: [UIColor whiteColor],UITextAttributeTextShadowColor: [UIColor clearColor],UITextAttributeTextShadowOffset: [NSValue valueWithUIOffset:UIOffsetMake(0.0f, 1.0f)],UITextAttributeFont: [UIFont fontWithName:@"ArialMT" size:18.0f] }]; CGFloat verticalOffset = -4; [[UINavigationBar appearance] setTitleVerticalPositionAdjustment:verticalOffset forBarMetrics:UIBarMetricsDefault]; } else { [[UINavigationBar appearance] setBarTintColor:[UIColor whiteColor]]; // 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:@"nav_bg_ios7.png"] forBarMetrics:UIBarMetricsDefault]; // Uncomment to change the back indicator image [[UINavigationBar appearance] setBackIndicatorImage:[UIImage imageNamed:@""]]; [[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:@"ArialMT" size:18.0], NSFontAttributeName, nil]]; CGFloat verticalOffset = -4; [[UINavigationBar appearance] setTitleVerticalPositionAdjustment:verticalOffset forBarMetrics:UIBarMetricsDefault]; } self.window.rootViewController = self.navigationController; [self.window makeKeyAndVisible]; return YES; }