[英]Custom NavigationBar iOS 5 compatibility
I was trying to implement a project with custom navigation bar; 我正在尝试使用自定义导航栏来实现一个项目。 however, the following code:
但是,以下代码:
@implementation UINavigationBar (CustomImage)
// Set the navigation bar background
- (void)drawRect:(CGRect)rect {
UIImage *image = [UIImage imageNamed:@"TopBar.png"];
[image drawInRect:CGRectMake(0, 0, self.frame.size.width, self.frame.size.height + 1)];
}
@end
is not working for iOS 5. I am using Xcode 4.2. 不适用于iOS5。我正在使用Xcode 4.2。
Question: How can I implement a custom navigation bar without compromising the use of the code above for earlier versions (< iOS 5)? 问题:如何实现自定义导航栏,而又不损害上述代码在早期版本(<iOS 5)中的使用?
Any help would be appreciated. 任何帮助,将不胜感激。 :)
:)
I finally got it...this might help others implementing this. 我终于明白了……这可能会帮助其他人实现这一目标。
@implementation UINavigationBar (CustomImage)
// Set the navigation bar background
- (UIImage *)barBackground{
UIImage *image = [UIImage imageNamed:@"TopBar.png"];
[image drawInRect:CGRectMake(0, 0, self.frame.size.width, self.frame.size.height + 1)];
return image;
}
- (void)didMoveToSuperview{
// Applies to iOS 5
if ([self respondsToSelector:@selector(setBackgroundImage:forBarMetrics:)])
{
[self setBackgroundImage:[self barBackground] forBarMetrics:UIBarMetricsDefault];
}
}
// This doesn't work on iOS5 but is needed for iOS4 and earlier
- (void)drawRect:(CGRect)rect
{
// Draw the image
[[self barBackground] drawInRect:rect];
}
@end
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.