如何在导航栏上显示背景图像或为本机iphone应用程序中的导航栏添加色调?
对于iOS5,请使用以下代码行:
UINavigationBar *navBar = [[self navigationController] navigationBar]; UIImage *backgroundImage = [UIImage imageNamed:@"nav-bar-background-normal"]; [navBar setBackgroundImage:backgroundImage forBarMetrics:UIBarMetricsDefault];
为了向后兼容,请检查导航栏是否响应setBackgroundImage:forBarMetrics:
更多信息:http: //sebastiancelis.com/2009/12/21/adding-background-image-uinavigationbar/
这就是我在iOS4上的表现:
#import// For .layer self.navigationController.navigationBar.layer.contents = (id)[UIImage imageNamed:@"navigationBarBackgroundImage"].CGImage; self.navigationController.navigationBar.tintColor = [UIColor orangeColor];
无需在z-orders(-exchangeSubviewAtIndex:withSubviewAtIndex :)之间切换子视图,在一行代码中设置背景图像和tintColor,并且也可以使用@ 2x图像.
一周前正在寻找这个.在此讨论发现了这一点.苹果.com/thread.jspa?threadID = 1649012&tstart = 0(抱歉不允许我发布真实链接).
-(void)setBackgroundImage:(UIImage*)image withTag:(NSInteger)bgTag{ if(image == NULL){ //might be called with NULL argument return; } UIImageView *aTabBarBackground = [[UIImageView alloc]initWithImage:image]; aTabBarBackground.frame = CGRectMake(0,0,self.frame.size.width,self.frame.size.height); aTabBarBackground.tag = bgTag; [self addSubview:aTabBarBackground]; [self sendSubviewToBack:aTabBarBackground]; [aTabBarBackground release]; } /* input: The tag you chose to identify the view */ -(void)resetBackground:(NSInteger)bgTag { [self sendSubviewToBack:[self viewWithTag:bgTag]]; }
我把它作为UINavigationBar的一个类别.要在UINavigationBarController中为UINavigationBar设置背景图像,我这样做了:
[navigationControllerForChannels.navigationBar setBackgroundImage:[UIImage imageNamed:@"top_bar.png"] withTag:48151623];
更新标签栏时我有些瑕疵,所以你要打电话
[self.navigationController.navigationBar resetBackground:48151623];
对酒吧进行任何修改后.