如何在iPhone导航栏上添加背景图片?
我想向导航栏添加图像背景。
对吗?
//set custom background image
UIImageView *backgroundView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"NavigationBackground.png"]];
[self.navigationBar insertSubview:backgroundView atIndex:0];
[backgroundView release];
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(12)
以下是提到的 链接 @luvieere 中的代码。
将此代码粘贴到上面的 rootview 控制器中
@implementation rootviewController
从 iOS 5 开始,有一个官方方法可以做到这一点。 (请参阅iOS 开发者库)
但是,仍然保留旧代码以实现向后兼容性,除非您真的想放弃 iOS 4 及更低版本。
Here's the code from the link @luvieere mentioned.
Paste this code into to the rootview controller just above
@implementation rootviewController
As of iOS 5, there is an official way to do this. (see iOS Developer Library)
But still, retain the old code for backward compatibility unless you really want to ditch iOS 4 and below.
仅靠您的代码是无法做到这一点的,您必须编写一个类别才能使其工作。关于如何执行此操作,有两种方法:第一种方法是将图像作为
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 yourUIViewController
'sviewDidAppear
method. This however has been reported having some issues with covering the rightUIBarButtonItem
. The other method involves overriding- (void)drawRect:(CGRect)rect
and drawing the image there. Both of these are extensively covered in this blog discussion..
最简单的方法就是设置 UINavigationBar 图层内容。
缺点是按钮不是根据正确的色调生成的,但您可以将导航栏颜色设置为最接近背景图像的颜色,并且应该注意色调。
The easiest way is just set the
UINavigationBar
layer contents.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.
我会在 appdelegate 中执行此操作,例如
在
.h 文件中:
I would do this in the appdelegate something like
And in
.h file:
在 ios5 中,我在 AppDelegate.m 中设置导航栏背景图像(对于所有导航栏图像):
in ios5 , I set the navigation bar background image(for all the navigation bar image) in
AppDelegate.m
:当 iPhone 5 到来时,我们必须设置这两种设备类型。因此,请使用此
了解更多信息,请访问此链接
When iphone 5 come we have to set both device type. So use this
For more info go to this link
这个在 iOS 6 和 7 上运行良好
This one working fine in iOS 6 and 7
Swift 版本:
更新了 Swift 3.2:
Swift version :
Updated Swift 3.2 :
您还可以添加一个扩展 UINavigationBar 类的类别,并重写该类别中的 drawRect: 方法。
You could also add a category that extends the UINavigationBar class and override the drawRect: method in the category.
我所做的只是使用我想要的图像创建一个 UIImage 并将其添加到导航栏,如下所示。
what i did was just created an UIImage with the image i wanted and added to the navigation bar like this.
在添加导航栏背景图像时,您应该非常小心其尺寸。请确保您具有适用于不同屏幕尺寸的以下尺寸。
1) 背景.png => 320x44
2) [电子邮件受保护] => 640x88 // 用于 iPhone5 和视网膜设备
3) [电子邮件受保护] => 1334x183 //用于iPhone6
使用以下代码添加背景图像并避免导航栏背景图像中的任何平铺。
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.
在您的 AppDelegate 类中尝试此代码以在导航栏显示图像。它会对你有很大帮助。
Try this code in your AppDelegate Class to show image at navigation bar. It will help you alot.