ios 如何在 iphone 导航栏上添加背景图片?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1692487/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-30 16:35:49  来源:igfitidea点击:

How to add background image on iphone Navigation bar?

iosiphonecocoa-touchbackgrounduinavigationbar

提问by Momi

I want to add an image background to my navigation bar.

我想为导航栏添加图像背景。

Is it right?

这样对吗?

//set custom background image
UIImageView *backgroundView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"NavigationBackground.png"]];
[self.navigationBar insertSubview:backgroundView atIndex:0];
[backgroundView release];

回答by iwat

Here's the code from the link@luvieere mentioned. Paste this code into to the rootview controller just above @implementation rootviewController

这是@luvieere 提到的链接中的代码。将此代码粘贴到上面的 rootview 控制器中 @implementation rootviewController

@implementation UINavigationBar (CustomImage)
- (void)drawRect:(CGRect)rect {
    UIImage *image = [UIImage imageNamed:@"NavigationBar.png"];
    [image drawInRect:CGRectMake(0, 0, self.frame.size.width, self.frame.size.height)];
}
@end

As of iOS 5, there is an official way to do this. (see iOS Developer Library)

从 iOS 5 开始,有一种官方方法可以做到这一点。(请参阅iOS 开发人员库

// someplace where you create the UINavigationController
if ([navigationController.navigationBar respondsToSelector:@selector(setBackgroundImage:forBarMetrics:)] ) {
    UIImage *image = [UIImage imageNamed:@"NavigationBar.png"];
    [navigationBar setBackgroundImage:image forBarMetrics:UIBarMetricsDefault];
}

But still, retain the old code for backward compatibility unless you really want to ditch iOS 4 and below.

但是,除非您真的想放弃 iOS 4 及更低版本,否则请保留旧代码以实现向后兼容性。

回答by luvieere

Your code alone won't do it, you'll have to write a category in order for it to work. There are two approaches regarding the way you should do it: the first one involves making the image a subview of your UINavigationBarand re-bringing it to front in each of your UIViewController's viewDidAppearmethod. This however has been reported having some issues with covering the right UIBarButtonItem. The other method involves overriding

仅靠您的代码是行不通的,您必须编写一个类别才能使其工作。关于您应该采用的方式有两种方法:第一种方法涉及使图像成为您的子视图,UINavigationBar并在您UIViewController的每个viewDidAppear方法中将其重新置于前面。然而,据报道这在覆盖权利方面存在一些问题UIBarButtonItem。另一种方法涉及覆盖

- (void)drawRect:(CGRect)rect

- (void)drawRect:(CGRect)rect

and drawing the image there. Both of these are extensively covered in this blogdiscussion..

并在那里绘制图像。这两个都在这个博客讨论中得到了广泛的介绍。

回答by drunknbass

The easiest way is just set the UINavigationBarlayer contents.

最简单的方法是设置UINavigationBar图层内容。

NSString *barBgPath = [[NSBundle mainBundle] pathForResource:@"mybgimage" ofType:@"png"];
[nBar.layer setContents: (id)[UIImage imageWithContentsOfFile: barBgPath].CGImage];

The downside is the buttons are not generated off the proper tint but you can set the navbar color to whatever is closest to your bg image and the tint should be taken care of.

缺点是按钮不是根据正确的色调生成的,但您可以将导航栏颜色设置为最接近您的 bg 图像的颜色,并且应该注意色调。

回答by Blazer

I would do this in the appdelegate something like

我会在 appdelegate 中这样做

+ (void)Generalstyle {
    //navigationbar
    UINavigationBar *navigationBar = [UINavigationBar appearance];
    [navigationBar setBackgroundImage:[UIImage imageNamed:@"navigation.png"] forBarMetrics:UIBarMetricsDefault];

}

And in

而在

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{

    [[self class] Generalstyle];

}

.h file:

.h 文件:

+ (void) Generalstyle;

回答by jianpx

in ios5 , I set the navigation bar background image(for all the navigation bar image) in AppDelegate.m:

在 ios5 中,我将导航栏背景图像(对于所有导航栏图像)设置为AppDelegate.m

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    [application setStatusBarStyle:UIStatusBarStyleBlackOpaque animated:NO];
    UIImage *navBarBackgroundImage = [UIImage imageNamed:@"nav_bg"];
    [[UINavigationBar appearance] setBackgroundImage:navBarBackgroundImage forBarMetrics:UIBarMetricsDefault];
    return YES;
}

回答by Isuru Jayathissa

When iphone 5 come we have to set both device type. So use this

当 iphone 5 到来时,我们必须设置两种设备类型。所以用这个

if([self.navigationController.navigationBar respondsToSelector:@selector(setBackgroundImage:forBarMetrics:)] ) {
//iOS 5 new UINavigationBar custom background
[self.navigationController.navigationBar setBackgroundImage:[UIImage imageNamed:@"navbg_ForiPhone5_Imagename.png"] forBarMetrics: UIBarMetricsDefault];
} else {
[self.navigationController.navigationBar insertSubview:[[UIImageView alloc] initWithImage:[UIImage imageNamed:@"navbg_ForOtherIphone_Imagename.png"]] atIndex:0];
}

For more info go to this link

有关更多信息,请转到此链接

回答by Gurumoorthy Arumugam

This one working fine in iOS 6 and 7

这个在 iOS 6 和 7 中运行良好

UINavigationBar *navBar = [[self navigationController] navigationBar];
  UIImage *backgroundImage = [UIImage imageNamed:@"nav-bar-background-normal"];
  [navBar setBackgroundImage:backgroundImage forBarMetrics:UIBarMetricsDefault];

回答by Babul Prabhakar

Swift version:

斯威夫特版

let navBar = UINavigationBar.appearance();
navBar.setBackgroundImage(UIImage(named: "yourImageName"), forBarMetrics: .Default)

Updated Swift 3.2:

更新 Swift 3.2

let navBar = UINavigationBar.appearance();
        navBar.setBackgroundImage(UIImage(named: "yourImageName"), for: .default)

回答by Aamir

While adding Navigation Bar background image, you should be very careful about its dimensions. Please make sure you have following dimensions for different screen sizes.

添加导航栏背景图像时,您应该非常注意其尺寸。请确保您有以下不同屏幕尺寸的尺寸。

1)background.png => 320x44
2)[email protected] => 640x88// used for iPhone5 and for retina devices
3)[email protected] => 1334x183// used for iPhone6

1)background.png => 320x44
2)[email protected] => 640x88// 用于 iPhone5 和视网膜设备
3)[email protected] => 1334x183// 用于 iPhone6

Use following code to add background image and to avoid any tiling in Navigation Bar's background image.

使用以下代码添加背景图像并避免在导航栏的背景图像中出现任何平铺。

[self.navigationController.navigationBar setBackgroundImage:[[UIImage imageNamed:@"background"] resizableImageWithCapInsets:UIEdgeInsetsMake(0, 0, 0, 0) resizingMode:UIImageResizingModeStretch] forBarMetrics:UIBarMetricsDefault];

回答by Shahzaib Maqbool

Try this code in your AppDelegate Class to show image at navigation bar. It will help you alot.

在您的 AppDelegate 类中尝试此代码以在导航栏中显示图像。它会帮助你很多。

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