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

发布于 2024-08-10 13:24:30 字数 291 浏览 6 评论 0 原文

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

对吗?

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

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(12

橘味果▽酱 2024-08-17 13:24:30

以下是提到的 链接 @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 及更低版本。

Here's the code from the link @luvieere mentioned.
Paste this code into to the rootview controller just above
@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)

// 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.

情域 2024-08-17 13:24:30

仅靠您的代码是无法做到这一点的,您必须编写一个类别才能使其工作。关于如何执行此操作,有两种方法:第一种方法是将图像作为 UINavigationBar 的子视图,并将其重新置于每个 UIViewController 的前面。的 viewDidAppear 方法。然而,据报道,这在覆盖正确的 UIBarButtonItem 时存在一些问题。另一种方法涉及重写

- (void)drawRect:(CGRect)rect

并在那里绘制图像。 此博客讨论中广泛讨论了这两个问题。

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 UINavigationBar and re-bringing it to front in each of your UIViewController's viewDidAppear method. This however has been reported having some issues with covering the right UIBarButtonItem. The other method involves overriding

- (void)drawRect:(CGRect)rect

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

一场信仰旅途 2024-08-17 13:24:30

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

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

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

The easiest way is just set the UINavigationBar layer contents.

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.

触ぅ动初心 2024-08-17 13:24:30

我会在 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;

I would do this in the appdelegate something like

+ (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:

+ (void) Generalstyle;
许仙没带伞 2024-08-17 13:24:30

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

in ios5 , I set the navigation bar background image(for all the navigation bar image) in 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;
}
梦与时光遇 2024-08-17 13:24:30

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

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

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

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

清风挽心 2024-08-17 13:24:30

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

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

This one working fine in iOS 6 and 7

UINavigationBar *navBar = [[self navigationController] navigationBar];
  UIImage *backgroundImage = [UIImage imageNamed:@"nav-bar-background-normal"];
  [navBar setBackgroundImage:backgroundImage forBarMetrics:UIBarMetricsDefault];
牵你的手,一向走下去 2024-08-17 13:24:30

Swift 版本

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)

Swift version :

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

Updated Swift 3.2 :

let navBar = UINavigationBar.appearance();
        navBar.setBackgroundImage(UIImage(named: "yourImageName"), for: .default)
短暂陪伴 2024-08-17 13:24:30

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

You could also add a category that extends the UINavigationBar class and override the drawRect: method in the category.

对岸观火 2024-08-17 13:24:30

我所做的只是使用我想要的图像创建一个 UIImage 并将其添加到导航栏,如下所示。

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

[[UINavigationBar appearance] setBackgroundImage:image forBarMetrics:UIBarMetricsDefault];

what i did was just created an UIImage with the image i wanted and added to the navigation bar like this.

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

[[UINavigationBar appearance] setBackgroundImage:image forBarMetrics:UIBarMetricsDefault];
二智少女猫性小仙女 2024-08-17 13:24:30

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

1) 背景.png => 320x44
2) [电子邮件受保护] => 640x88 // 用于 iPhone5 和视网膜设备
3) [电子邮件受保护] => 1334x183 //用于iPhone6

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

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

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

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];
南城追梦 2024-08-17 13:24:30

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

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

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

[[UINavigationBar appearance] setBackgroundImage:[[UIImage imageNamed:@"headerImg.png"] resizableImageWithCapInsets:UIEdgeInsetsMake(0, 0, 0, 0)] forBarMetrics:UIBarMetricsDefault];
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文