使用以下代码设置UINavigationBar的外观,但在我的情况下,当视图控制器第一次显示时,它未设置。我只需要推动另一个视图控制器然后设置它。
UIImage *gradientImage44 = [[UIImage imageNamed:@"navigationBar44"] resizableImageWithCapInsets:UIEdgeInsetsMake(0, 0, 0, 0)];
UIImage *gradientImage32 = [[UIImage imageNamed:@"navigationBar32"] resizableImageWithCapInsets:UIEdgeInsetsMake(0, 0, 0, 0)];
[[UINavigationBar appearance] setBackgroundImage:gradientImage44 forBarMetrics:UIBarMetricsDefault];
[[UINavigationBar appearance] setBackgroundImage:gradientImage32 forBarMetrics:UIBarMetricsLandscapePhone];
答案 0 :(得分:0)
有不同的方法可以为ios 5及以上版本设置Uinavigationbar背景图像,并且ios低于ios 5
对于ios 5,您可以在app delegate中使用以下代码
//Customize the look of the UINavBar for iOS5 devices
UIImage *gradientImage44 = [[UIImage imageNamed:@"navigationBar44"]
resizableImageWithCapInsets:UIEdgeInsetsMake(0, 0, 0, 0)];
UIImage *gradientImage32 = [[UIImage imageNamed:@"navigationBar32"]
resizableImageWithCapInsets:UIEdgeInsetsMake(0, 0, 0, 0)];
[[UINavigationBar appearance] setBackgroundImage:gradientImage44
forBarMetrics:UIBarMetricsDefault];
[[UINavigationBar appearance] setBackgroundImage:gradientImage32
forBarMetrics:UIBarMetricsLandscapePhone];
但如果您使用的任何ios版本低于ios 5,则必须继承UINavigationControl并重载drawRect,如
@interface CustomNavigationBar : UINavigationBar
@end
@implementation CustomNavigationBar
-(void) drawRect:(CGRect)rect
{
UIImage *image = [UIImage imageNamed: @"myNavBarImage"];
[image drawInRect:CGRectMake(0, 0, self.frame.size.width, self.frame.size.height)];
}
@end
// this can go anywhere
+(UINavigationController*) myCustomNavigationController
{
YOurViewController *vc = [[[YOurViewController alloc] init] autorelease];
UINavigationController *nav = [[[NSBundle mainBundle]
loadNibNamed:@"CustomNavigationController" owner:self options:nil] objectAtIndex:0];
nav.viewControllers = [NSArray arrayWithObject:vc];
return nav;
}
像这样,您可以自定义导航栏,并且对所有ios版本都有效。
您可以使用
检查ios 5if ([[UINavigationBar class]respondsToSelector:@selector(appearance)]) {
///ios 5 and above.
}