2009-11-07 238 views
32

我想为我的导航条添加图像背景如何在iphone导航栏上添加背景图片?

是不是正确?

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

我更喜欢这个代码,因为不推荐使用类别。如果你使用它,你会发现它工作正常。 – Steve

回答

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的这个代码粘贴到到rootview控制器,还有就是做这个的官方途径。 (见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或以下。

+2

不要忘记加入@luvieere,他值得信任。 – iwat

0

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

+0

这样的代码是正确的?我还没有尝试! :D – Momi

+0

很难说。你会在哪里使用它?您可能需要在每次显示酒吧时将背景图片视图展示在前面 – nduplessis

32

您的代码本身不会这样做,您必须编写一个类别才能使其工作。关于你应该怎么做的方法有两种方法:第一种方法是将图像作为UINavigationBar的子视图,并将其重新放在每个UIViewControllerviewDidAppear方法中。然而,这已被报道与覆盖右边UIBarButtonItem有一些问题。另一种方法包括首要

- (void)drawRect:(CGRect)rect

和绘制图像那里。这两个在this blog讨论中被广泛地包括。

9

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

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

的缺点是按键也不会产生过正确的色彩,但你可以设置导航栏的颜色最接近什么是你的背景图片和色调应采取照顾。

+0

这种方法非常容易,并且可以为用于绘图的图像提供更多的灵活性。我刚刚尝试过,它效果很好!只要记得导入'QuartzCore'库。 – FreeAsInBeer

+0

很好,谢谢。我试图避开类别,我发现它们不可靠,所以这很好。 – Chris

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

欲了解更多信息请访问在iOS5的这个link

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; 
} 
+1

使用代理外观的+1 –

0

什么我做的是刚刚创建了一个UIImage与我想要的图像,并添加到这样的导航栏。

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

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

这一个在IOS 6工作正常和7

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

夫特版本

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

更新夫特3.2

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

在添加导航栏背景图片时,您应该非常小心它的尺寸。请确保您拥有不同屏幕尺寸的以下尺寸。

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

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

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

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

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