32

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

这样对吗?

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

12 回答 12

98

这是来自@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

从 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];
}

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

于 2009-12-04T14:18:57.970 回答
32

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

- (void)drawRect:(CGRect)rect

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

于 2009-11-07T11:56:19.357 回答
9

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

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

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

于 2010-08-31T19:08:47.083 回答
8

我会在 appdelegate 中执行此操作,例如

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

}

而在

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

    [[self class] Generalstyle];

}

.h 文件:

+ (void) Generalstyle;
于 2012-07-31T15:51:52.967 回答
8

在 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;
}
于 2013-08-08T03:26:45.407 回答
2

当 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];
}

欲了解更多信息,请访问此链接

于 2012-12-19T12:38:43.547 回答
2

这在 iOS 6 和 7 中运行良好

UINavigationBar *navBar = [[self navigationController] navigationBar];
  UIImage *backgroundImage = [UIImage imageNamed:@"nav-bar-background-normal"];
  [navBar setBackgroundImage:backgroundImage forBarMetrics:UIBarMetricsDefault];
于 2014-02-12T12:09:43.870 回答
1

斯威夫特版本

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

更新了 Swift 3.2

let navBar = UINavigationBar.appearance();
        navBar.setBackgroundImage(UIImage(named: "yourImageName"), for: .default)
于 2016-04-12T20:34:54.273 回答
0

您还可以添加一个扩展 UINavigationBar 类的类别并覆盖该类别中的 drawRect: 方法。

于 2009-11-07T09:08:06.393 回答
0

我所做的只是用我想要的图像创建了一个 UIImage 并像这样添加到导航栏中。

UIImage *image = [UIImage imageNamed:@"yourImage.png"];

[[UINavigationBar appearance] setBackgroundImage:image forBarMetrics:UIBarMetricsDefault];
于 2013-09-04T19:54:22.287 回答
0

在添加导航栏背景图像时,您应该非常小心其尺寸。请确保您具有适用于不同屏幕尺寸的以下尺寸。

1) background.png => 320x44
2) background@2x.png => 640x88 // 用于 iPhone5 和 Retina 设备
3) background@3x.png => 1334x183 // 用于 iPhone6

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

[self.navigationController.navigationBar setBackgroundImage:[[UIImage imageNamed:@"background"] resizableImageWithCapInsets:UIEdgeInsetsMake(0, 0, 0, 0) resizingMode:UIImageResizingModeStretch] forBarMetrics:UIBarMetricsDefault];
于 2017-02-27T06:52:31.037 回答
0

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

[[UINavigationBar appearance] setBackgroundImage:[[UIImage imageNamed:@"headerImg.png"] resizableImageWithCapInsets:UIEdgeInsetsMake(0, 0, 0, 0)] forBarMetrics:UIBarMetricsDefault];
于 2017-03-30T17:02:07.117 回答