首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >iOS 7导航条背景图像问题

iOS 7导航条背景图像问题
EN

Stack Overflow用户
提问于 2014-02-18 12:58:46
回答 2查看 4K关注 0票数 2

我使用图像作为导航条背景图像。要设置图像,我使用了以下代码:

代码语言:javascript
复制
[[UINavigationBar appearance] setBackgroundImage:[UIImage imageNamed:@"nav_logo_ios7.png"] forBarMetrics:UIBarMetricsDefault];

对于iOS7 "nav_logo_ios7.png“图像大小为768x64,对于iOS6和bellow,我使用的图像大小为768x44。

这在所有UIViewControllers上都运行得很好。

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

我怎么能处理这事?

提前谢谢。

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2014-02-18 13:52:09

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

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

首先,检查设备运行的iOS版本的官方更好的方法是这样做:

代码语言:javascript
复制
if (NSFoundationVersionNumber > NSFoundationVersionNumber_iOS_6_1)
{
    //handle iOS 7 Stuff
}
else
{
    //handle older iOS versions
}

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

UINavigationBar背景图像不应该是一幅固定大小的图像,而应该是可伸缩的图像,例如可重复的图案,所以也许这将是重新思考未来设计的一个想法。然而,如果你真的想继续一个定制的固定大小的图像,那么我有一个建议给你.

UINavigationController允许您使用自定义UINavigationBar和UIToolbar类使用initWithNavigationBarClass:toolbarClass:初始化实例.这意味着您可以在任何视图中插入与正在以模型方式呈现的视图不同的UINavigationBar子类。

这意味着,如果您的导航控制器是否以模态方式显示,您将能够指定依赖于此的不同背景图像,例如:

代码语言:javascript
复制
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应用程序。

票数 2
EN

Stack Overflow用户

发布于 2014-02-18 13:24:19

代码语言:javascript
复制
//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;
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/21854588

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档