iOS 7 Navigationbar背景图片问题

时间:2014-02-18 12:58:47

标签: ios ios7 uinavigationbar uiactivityviewcontroller

我使用Image作为导航栏背景图片。要设置Image,我使用以下代码:

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

对于iOS7“nav_logo_ios7.png”图片大小为768x64,对于iOS6和以下我用过的图片大小为768x44。

这适用于所有UIViewControllers

enter image description here

在同一个项目中,我正在使用UIActivityViewController。在iOS7上,邮件撰写视图如下所示:

enter image description here

我该如何处理?

提前致谢。

2 个答案:

答案 0 :(得分:2)

您面临的问题是,当以模态方式呈现UIViewController时,状态栏不会包含在UINavigationBar的高度中。

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

首先,官方以及检查设备运行的iOS版本的更好方法是执行以下操作:

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

有关详细信息,请查看NSObjCRuntime.h标题。

UINavigationBar背景图片不应该是固定尺寸的图像,而应该是可伸缩的图像,例如可重复的图案,所以也许重新考虑未来的设计是个主意...但是如果你想继续定制的话固定大小的图像然后我有一个建议给你...

UINavigationController允许您使用initWithNavigationBarClass:toolbarClass:初始化具有自定义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不是真正的视图控制器,因此尝试使用自定义导航栏子类初始化它可能无效。这就是我使用自定义导航的原因所有非模态导航控制器的bar子类,而不是相反。

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

正如我之前所说的那样...... UINavigationBars并不是真的设计为具有固定大小的背景图像(这就是为什么它很难实现)所以如果你觉得这个工作太复杂了,那么它可能会很好想重新考虑你的设计。

最后一件事(我保证)... iOS 7中的一个主要设计变化是让你的导航栏中的内容流过状态栏。添加图像以防止这种情况并将其替换为对于iOS 7应用程序,纯白色背景看起来很奇怪。

答案 1 :(得分:0)

//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;
}