天天看點

ios5 自定義導覽列問題 UINavigationBar

在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      

複制代碼