UINavigationBar的的drawRect没有在安装iOS 5.0被称为 [英] UINavigationBar's drawRect is not called in iOS 5.0
问题描述
我overrided(放置于类别或交叉混合)UINavigationBar的的drawRect显示自定义背景。在iOS 5中它不工作。我应该怎么办?
I've overrided(placed in category, or swizzled) UINavigationBar's drawRect to show custom background. In iOS 5 it's not working. What should I do?
推荐答案
有一些可能的解决方案:
There's some possible solutions:
最快的修为最懒惰的我们:
Quickest fix For laziest of us :
@interface MyNavigationBar : UINavigationBar
@end
@implementation MyNavigationBar
- (void)drawRect:(CGRect)rect {
}
@end
@implementation UINavigationBar (BecauseIMLazyHacks)
/*
Another Ugly hack for iOS 5.0 support
*/
+ (Class)class {
return NSClassFromString(@"MyNavigationBar");
}
-(void)drawRect:(CGRect)rect {
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextTranslateCTM(context, 0, self.frame.size.height);
CGContextScaleCTM(context, 1.0, -1.0);
CGContextDrawImage(context, CGRectMake(0, 0,
self.frame.size.width, self.frame.size.height), barBackground.CGImage);
}
@end
再。它的工作原理,但你不应该做这样的。
Again. It works, but You shouldn't do it like this.
的另一种方法,如在WWDC'11建议是覆盖UINavigationBar的(创建MyNavigationBar)和从XIB初始化的UINavigationController像这里:
Another way, as suggested in WWDC'11 is to override UINavigationBar (Create MyNavigationBar) and initialize UINavigationController from xib like here :
<一个href=\"http://www.iosdevnotes.com/2011/09/custom-uinavigationbars-techniques/\">http://www.iosdevnotes.com/2011/09/custom-uinavigationbars-techniques/
最后,使用了iOS5.0和iOS5.0-逻辑流量开关
使用新的API,它是可能的。
And finally, use logic flow switch for iOS5.0 and iOS5.0- Use new API where it's possible.
分类是错误的道路,交叉混合是走错了路。 (他们只是在耳语你的耳朵:,给自己的黑暗面是你可以保存你的应用程序的唯一方法)
Categories is wrong path, Swizzling is wrong path. (They're just whispering in your ears:"Give yourself to the Dark Side. It is the only way you can save your apps.")
这篇关于UINavigationBar的的drawRect没有在安装iOS 5.0被称为的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!