iOS 7导航栏背景图像问题
我使用图像作为导航栏背景图像。为了设置图像,我使用了以下代码:iOS 7导航栏背景图像问题,ios,ios7,uinavigationbar,uiactivityviewcontroller,Ios,Ios7,Uinavigationbar,Uiactivityviewcontroller,我使用图像作为导航栏背景图像。为了设置图像,我使用了以下代码: [[UINavigationBar appearance] setBackgroundImage:[UIImage imageNamed:@"nav_logo_ios7.png"] forBarMetrics:UIBarMetricsDefault]; 对于iOS7,“nav_logo_iOS7.png”图像大小为768x64,对于iOS6和以下版本,我使用的图像大小为768x44 这在所有UIViewControllers上运行
[[UINavigationBar appearance] setBackgroundImage:[UIImage imageNamed:@"nav_logo_ios7.png"] forBarMetrics:UIBarMetricsDefault];
对于iOS7,“nav_logo_iOS7.png”图像大小为768x64,对于iOS6和以下版本,我使用的图像大小为768x44
这在所有UIViewControllers
上运行良好
在同一个项目中,我使用的是UIActivityViewController
。在iOS7上,邮件撰写视图如下所示:
if (NSFoundationVersionNumber > NSFoundationVersionNumber_iOS_6_1)
{
//handle iOS 7 Stuff
}
else
{
//handle older iOS versions
}
我该怎么办
提前感谢。//在'AppDelegate.m中`
//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;
}
-(BOOL)应用程序:(UIApplication*)应用程序使用选项完成启动:(NSDictionary*)启动选项
{
如果([[[UIDevice currentDevice]系统版本]浮点值]<7.0)
{
[[UINavigationBar外观]setBackgroundImage:[UIImage ImageName:@“nav_bg.png”]forBarMetrics:UIBarMetricsDefault];
[[UINavigationBar外观]setTitleTextAttributes:
@{
UITextAttributeTextColor:[UIColor whiteColor],UITextAttributeTextShadowColor:[UIColor clearColor],UITextAttributeTextShadowOffset:[NSValue valueWithUIOffset:UIOffsetMake(0.0f,1.0f)],UITextAttributeFont:[UIFont fontWithName:@“ArialMT”大小:18.0f]
}];
CGFloat垂直偏移=-4;
[[UINavigationBar外观]setTitleVerticalPositionAdjustment:BarMetrics的垂直偏移量:UIBarMetricsDefault];
}
其他的
{
[[UINavigationBar外观]SetPartIntColor:[UIColor whiteColor]];
//取消注释以更改“后退”按钮的颜色
[[UINavigationBar外观]setTintColor:[UIColor whiteColor]];
//取消注释以指定自定义背景图像
[[UINavigationBar外观]setBackgroundImage:[UIImage ImageName:@“nav_bg_ios7.png”]forBarMetrics:UIBarMetricsDefault];
//取消注释以更改后指示器图像
[[UINavigationBar外观]设置指示符号图像:[UIImage ImageName:@”“];
[[UINavigationBar外观]收件指示符号转换图像:[UIImage ImageName:@”“];
//取消注释以更改标题的字体样式
NSShadow*shadow=[[NSShadow alloc]init];
shadow.shadowColor=[UIColor colorWithRed:0.0绿色:0.0蓝色:0.0 alpha:0.8];
shadow.shadowOffset=CGSizeMake(0,1);
[[UINavigationBar外观]setTitleTextAttributes:[NSDictionary Dictionary With Objects and Keys:[UIColor ColorWith红色:245.0/255.0绿色:245.0/255.0蓝色:245.0/255.0 alpha:1.0],NSForegroundColorAttributeName,shadow,NSShadowAttributeName,[UIFontWithName:@“ArialMT”大小:18.0],NSFontAttributeName,nil];
CGFloat垂直偏移=-4;
[[UINavigationBar外观]setTitleVerticalPositionAdjustment:BarMetrics的垂直偏移量:UIBarMetricsDefault];
}
self.window.rootViewController=self.navigationController;
[self.window makeKeyAndVisible];
返回YES;
}
您面临的问题是,当以模式显示UIViewController时,状态栏不包括在UINavigationBar的高度中
这意味着64pt图像不正确
首先,检查设备运行的iOS版本的官方更好方法是执行以下操作:
if (NSFoundationVersionNumber > NSFoundationVersionNumber_iOS_6_1)
{
//handle iOS 7 Stuff
}
else
{
//handle older iOS versions
}
有关更多信息,请查看NSObjCRuntime.h
标题
UINavigationBar背景图像不应该是固定大小的图像,而应该是可拉伸的图像,例如可重复的图案,因此也许可以重新考虑未来的设计。。。然而,如果你想继续使用自定义的固定大小的图像,那么我有一个建议给你
UINavigationController允许您使用自定义UINavigationBar和UIToolbar类初始化实例。。。这意味着您可以使用不同的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并不是真正设计成具有固定大小的背景图像(这就是为什么很难实现的原因),所以如果您认为这项工作过于复杂,那么重新考虑您的设计可能是一个好主意
还有最后一件事(我保证)。。。iOS 7的一个主要设计变化是让导航栏中的内容在状态栏下方流动。。对于iOS 7应用程序来说,添加一个图像以防止出现这种情况并用纯白背景替换它似乎很奇怪。如果([[[[UIDevice currentDevice]systemVersion]floatValue]>=7.0){//load iOS 7 image}或者{//load iOS 6 image}-您需要按照上述要求加载图像code@bhavyakothari这就是我正在做的。非常感谢你的解决方案。