我想在导航栏中添加图像背景。
是不是?
//set custom background image
UIImageView *backgroundView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"NavigationBackground.png"]];
[self.navigationBar insertSubview:backgroundView atIndex:0];
[backgroundView release];
答案 0 :(得分:98)
以下是link @luvieere提到的代码。
将此代码粘贴到上面的根视图控制器中
@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 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];
}
但是,为了向后兼容,请保留旧代码,除非你真的想放弃iOS 4及以下版本。
答案 1 :(得分:32)
您的代码本身不会这样做,您必须编写一个类别才能使其正常工作。关于你应该采用的方法有两种方法:第一种方法是将图像作为UINavigationBar
的子视图,并在每个UIViewController
viewDidAppear
中将其重新置于前面。 } 方法。然而,据报道,这涉及到涵盖权利UIBarButtonItem
的一些问题。另一种方法涉及覆盖
- (void)drawRect:(CGRect)rect
并在那里绘制图像。 this blog讨论中广泛涵盖了这两个方面。
答案 2 :(得分:9)
最简单的方法是设置UINavigationBar
图层内容。
NSString *barBgPath = [[NSBundle mainBundle] pathForResource:@"mybgimage" ofType:@"png"];
[nBar.layer setContents: (id)[UIImage imageWithContentsOfFile: barBgPath].CGImage];
缺点是按钮不是从正确的色调生成的,但您可以将导航栏颜色设置为最接近您的bg图像的颜色,并且应该处理色调。
答案 3 :(得分: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;
答案 4 :(得分:8)
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;
}
答案 5 :(得分: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];
}
有关详细信息,请转到此link
答案 6 :(得分:2)
这个在iOS 6和7中工作得很好
UINavigationBar *navBar = [[self navigationController] navigationBar];
UIImage *backgroundImage = [UIImage imageNamed:@"nav-bar-background-normal"];
[navBar setBackgroundImage:backgroundImage forBarMetrics:UIBarMetricsDefault];
答案 7 :(得分:1)
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)
答案 8 :(得分:0)
您还可以添加扩展UINavigationBar类的类别,并覆盖类别中的drawRect:方法。
答案 9 :(得分:0)
我所做的只是用我想要的图像创建了一个UIImage并添加到导航栏中。
UIImage *image = [UIImage imageNamed:@"yourImage.png"];
[[UINavigationBar appearance] setBackgroundImage:image forBarMetrics:UIBarMetricsDefault];
答案 10 :(得分:0)
添加导航栏背景图像时,您应该非常小心其尺寸。请确保您有不同屏幕尺寸的以下尺寸。
1) background.png =>的 320x44 强>
2) background@2x.png => 640x88 //用于iPhone5和视网膜设备
3) background@3x.png => 1334x183 //用于iPhone6
使用以下代码添加背景图像并避免在导航栏的背景图像中进行任何平铺。
[self.navigationController.navigationBar setBackgroundImage:[[UIImage imageNamed:@"background"] resizableImageWithCapInsets:UIEdgeInsetsMake(0, 0, 0, 0) resizingMode:UIImageResizingModeStretch] forBarMetrics:UIBarMetricsDefault];
答案 11 :(得分:0)
在AppDelegate类中尝试此代码,以在导航栏中显示图像。它会帮助你很多。
[[UINavigationBar appearance] setBackgroundImage:[[UIImage imageNamed:@"headerImg.png"] resizableImageWithCapInsets:UIEdgeInsetsMake(0, 0, 0, 0)] forBarMetrics:UIBarMetricsDefault];