iOS 7中如何设置导航栏图片

14

我希望将我的当前项目从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中设置导航栏图片?


感谢所有的建议。我已经找到了我的问题的解决方案,但我不确定这是否是正确的方式。目前它对我来说是有效的。 - Mayuri R Talaviya
有人能给出答案吗?http://stackoverflow.com/questions/16190127/custom-uinavigationbar-background-image-appears-darker-than-the-original-image - Mashhadi
9个回答

31

请在AppDelegate中尝试添加以下代码

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

这是Swift版本:

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

Swift 3 版本:

UINavigationBar.appearance().setBackgroundImage(UIImage.init(named: "logo-dark.png"), for: UIBarMetrics.default)

只要按照iOS7指南中提到的规则创建用作导航栏背景的图像,就可以正常工作: https://developer.apple.com/library/ios/documentation/UserExperience/Conceptual/MobileHIG/ResizableImages.html#//apple_ref/doc/uid/TP40006556-CH30-SW1 - Sandip Patel - SM
1
只需将此代码添加到AppDelegate中的'applicationDidFinishLaunchingWithOptions'方法即可。 - Gokul

15

对于iOS 7:

[self.navigationController.navigationBar setBackgroundImage:[UIImage imageNamed:@"navbar.png"] forBarMetrics:UIBarMetricsDefault];

5
使用这个简单的语法轻松更改导航栏背景:Navigation Background
self.navigationController.navigationBar.barTintColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"YourImage.png"]];
self.navigationController.navigationBar.titleTextAttributes = @{NSForegroundColorAttributeName: [UIColor whiteColor]};

4
if ([self.navigationController.navigationBar respondsToSelector:@selector(setBackgroundImage:forBarMetrics:)] )
{

    UIImage *image = [UIImage imageNamed:@"navigation.png"];
    [self.navigationController.navigationBar setBackgroundImage:image forBarMetrics:UIBarMetricsDefault];
}

3

使用Storyboard的方法:

  1. 将一个Image View控件拖到Storyboard场景的底部栏。
  2. 在场景列表中,从左侧的导航项上控制拖动到新创建的图像视图。
  3. 点击图像视图,并在属性中设置要显示的图片。

1

只需这样做...

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

1

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

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

更多信息请访问链接:
https://developer.apple.com/library/ios/documentation/UserExperience/Conceptual/MobileHIG/ResizableImages.html#//apple_ref/doc/uid/TP40006556-CH30-SW1


谢谢!那正是我需要的。 - Politta

0

在 appDelegate 类中尝试这段代码,它会帮助你。

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

0
- (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];
    }



}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接