Tor*_*ups 2 objective-c uinavigationbar
我的大多数应用程序都有一个静态UINavigationBar,但最后我有一些视图控制器需要根据视图控制器本身的某些属性动态设置背景图像.
我目前必须设置的代码工作正常,除了动态部分.是否可以在运行时覆盖drawRect方法以动态设置背景图像?
@implementation UINavigationBar (UINavigationBarCategory)
- (void)drawRect:(CGRect)rect {
UIColor *color = [UIColor blackColor];
UIImage *img = [UIImage imageNamed: @"nav.png"];
[img drawInRect:CGRectMake(0, 0, self.frame.size.width, self.frame.size.height)];
self.tintColor = color;
}
@end
Run Code Online (Sandbox Code Playgroud)
使用类别覆盖方法是一件错误的事情,即使它适用于这种特殊情况.
如果您在NIB中实例化了导航控制器,则可以将UINavigationBar子类化,并在Interface Builder中将导航控制器的bar类设置为您自己的类.一旦这样做,您就可以更好地控制导航栏的外观.
@interface MyNavigationBar : UINavigationBar
{
UIImage* customImage;
}
@property(nonatomic,retain) UIImage* customImage;
@end
@implementation MyNavigationBar
-(void)drawRect:(CGRect)rect
{
if (self.customImage) {
// draw your own image
} else {
[super drawRect:rect];
}
}
@end
Run Code Online (Sandbox Code Playgroud)
请记住使"自定义"视图控制器将customImage属性重置为nil viewWillDisappear:.
| 归档时间: |
|
| 查看次数: |
7341 次 |
| 最近记录: |