ios5 自定义导航条的问题
来源:互联网 发布:服装店收银软件排行 编辑:程序博客网 时间:2024/05/16 13:00
在ios5之前的系统中,可以通过定义导航条类别的方式自定义导航条:
if ([navBar respondsToSelector:@selector(setBackgroundImage:forBarMetrics:)])
{
// set globablly for all UINavBars
[[UINavigationBar appearance] setBackgroundImage:[UIImage imageNamed:@"brnlthr_nav.jpg"] forBarMetrics:UIBarMetricsDefault];
// ...
注:参考整理自 http://stackoverflow.com/questions/7657465/uinavigationbars-drawrect-is-not-called-in-ios-5-0
@implementation UINavigationBar (CustomImage)- (void)drawRect:(CGRect)rect { // Drawing code UIImage *image = [[UIImage imageNamed:@"header.png"] retain]; [image drawInRect:CGRectMake(0, 0,self.frame.size.width , self.frame.size.height)]; [image release]; }@end
但在ios5中,这种方式不起作用了。详见ios 5.0发布说明:
http://developer.apple.com/library/ios/#releasenotes/General/RN-iOSSDK-5_0/_index.html#//apple_ref/doc/uid/TP40010949
在iOS 5中,UINavigationBar, UIToolbar, and UITabBar 的实现方式改变了,因此drawRect:方法不会被调用了。除非在他们的子类中实现。
因此,要在iOS 5.0中继续使用自定义的导航条,这里提供两种方法:
1、使用5.0中新提供的UINavigationBar的方法setBackgroundImage:forBarMetrics:来设置背景。
但是为了与4.0等系统兼容,在使用该方法前必须先进行判断:(在5.0之前的系统中继续使用原来的方法)
(需要在每个用到navigationbar的地方都调用该方法,可能改动的地方比较多)
UINavigationBar *navBar = [myNavController navigationBar];if ([navBar respondsToSelector:@selector(setBackgroundImage:forBarMetrics:)])
{
// set globablly for all UINavBars
[[UINavigationBar appearance] setBackgroundImage:[UIImage imageNamed:@"brnlthr_nav.jpg"] forBarMetrics:UIBarMetricsDefault];
// ...
}
2、使用UINavigationBar的子类的方式来实现:(用这种方式可以不用对每个使用navigationbar的地方都进行修改,属于懒人做法)
@interface MyNavigationBar : UINavigationBar@end@implementation MyNavigationBar- (void)drawRect:(CGRect)rect { [super drawRect:rect];}@end@implementation UINavigationBar (LazyNavigationBar)+ (Class)class { return NSClassFromString(@"MyNavigationBar");}-(void)drawRect:(CGRect)rect { UIImage *backImage = [UIImage imageNamed:@"backNav.png"]; [backImage drawInRect:CGRectMake(0, 0, self.frame.size.width, self.frame.size.height)]; }@end
注:参考整理自 http://stackoverflow.com/questions/7657465/uinavigationbars-drawrect-is-not-called-in-ios-5-0
- ios5 自定义导航条的问题
- ios5 自定义导航条问题
- ios5 自定义导航条问题
- ios5 自定义导航条问题
- ios5 自定义导航条问题 UINavigationBar
- ios5 自定义导航条问题 UINavigationBar
- ios5 自定义导航条问题 UINavigationBar
- ios5自定义导航条
- 自定义导航条时返回按钮失效的问题
- 自定义导航条的颜色
- 自定义导航条滑动返回问题
- 自定义导航条的viewpager+fragment
- 自定义导航条1
- android 自定义导航条
- 自定义快速导航条
- iOS7新特性-完美解决iOS7关于自定义导航条UIBarButtonItem偏移的问题
- UIViewController上自定义导航条(避免每个controller上写自定义的导航条)
- 解决iOS7上自定义导航条UIBarButtonItem偏移问题
- 数据库 TRUNCATE TABLE 语句
- c++引用
- inputAccessoryView的使用
- Loki之类型识别
- c++多线程(一)
- ios5 自定义导航条的问题
- web_add_auto_header("x-requested-with", "XMLHttpRequest "); 在LR中的作用
- CBW、CWDE、CDQ、CWD: 扩展指令
- android 获取 imei号码
- JMF getDeviceLIst 得到null解决方案
- linux log 日志编程
- 在asp.net程序里调整ado.net连接池
- 今日事,今日毕
- c++多线程(二)