【问题标题】:custom UINavigation bar in iOS5iOS5中的自定义导航栏
【发布时间】:2011-10-18 11:24:50
【问题描述】:
更新到 Xcode 4.2 后我遇到了问题。
在我使用以下代码制作自定义导航栏之前,但是当我使用 iPhone 5.0 模拟器时,它会失败,而在 iPhone 4.2 模拟器中它没问题。
我可以知道是什么问题吗?我该如何解决这个问题?
非常感谢
@implementation UINavigationBar (UINavigationBarCustomDraw)
- (void) drawRect:(CGRect)rect {
[self setTintColor:[UIColor colorWithRed:0.4f
green: 0.0f
blue:0.4f
alpha:1]];
if ([self.topItem.title length] > 0 && ![self.topItem.title isEqualToString:@""])
{
[[UIImage imageNamed:@"purple.jpg"] drawInRect:rect];
}
}
@end
【问题讨论】:
标签:
uinavigationbar
xcode4.2
【解决方案1】:
如果您需要带有一些图像的自定义 UINavigationbar,那么您需要将此代码放入作为导航堆栈的第一个视图的 rootViewController(A > B > C,因此您必须将其放入 A)
- (void)viewDidLoad
{
[super viewDidLoad];
[[UIDevice currentDevice] systemVersion];
if ([[[UIDevice currentDevice] systemVersion] floatValue] > 4.9) {
//iOS 5
UIImage *toolBarIMG = [UIImage imageNamed: @"purple.jpg"];
if ([self.navigationController.navigationBar respondsToSelector:@selector(setBackgroundImage:forBarMetrics:)]) {
[[UINavigationBar appearance] setBackgroundImage:toolBarIMG forBarMetrics:UIBarMetricsDefault];
}
} else {
//iOS 4
[self.navigationController.navigationBar insertSubview:[[[UIImageView alloc] initWithImage:[UIImage imageNamed:@"purple.jpg"]] autorelease] atIndex:0];
}
}
}