ios5 自定義導航條問題


在ios5之前的系統中,可以通過定義導航條類別的方式自定義導航條:

@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的地方都調用該方法,可能改動的地方比較多)

 1 UINavigationBar *navBar = [myNavController navigationBar];
2 if ([navBar respondsToSelector:@selector(setBackgroundImage:forBarMetrics:)])
3 {
4 // set globablly for all UINavBars
5 [[UINavigationBar appearance] setBackgroundImage:[UIImage imageNamed:@"brnlthr_nav.jpg"] forBarMetrics:UIBarMetricsDefault];
6   // ...

 

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


3.使用category 范疇   來實現

 

@implementationUINavigationBar(MyCustomNavBar)

-(void)setBackgroudImage:(UIImage*)image
{
CGSize imageSize =[image size];
self.frame =CGRectMake(self.frame.origin.x,self.frame.origin.y,self.frame.size.width, imageSize.height);
UIImageView*backgroundImage =[[UIImageView alloc] initWithImage:image];
backgroundImage.frame =self.bounds;
[self addSubview:backgroundImage];
[backgroundImage release];
}
@end
//The above swizzling will allow you to set any custom background image for the UINavigationBar(iOS5 & iOS4).

 

 

Here's a less-ugly solution that works for both iOS4 and 5:

@implementationUINavigationBar(CustomBackground)

-(UIImage*)barBackground
{
return[UIImage imageNamed:@"top-navigation-bar.png"];
}

-(void)didMoveToSuperview
{
//iOS5 only
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 image
[[self barBackground] drawInRect:rect];
}

@end



 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM