2011-12-20 106 views
-1

我已經在我們的項目中使用xcode 4(ios 4.3)實現了自定義的導航和工具欄,它的工作很好,但現在我已經更新了我的xcode 4.2(ios 5),這裏它不工作。事情很奇怪,請在這一個導航和工具欄自定義背景圖像不工作在IOS 5

幫助這裏是我的應用程序委託代碼

@implementation UINavigationBar (CustomImage) 
- (void)drawRect:(CGRect)rect { 
    UIImage *image = [UIImage imageNamed: @"TopBg_with_logo.png"]; 
    [image drawInRect:CGRectMake(0, 0, self.frame.size.width, self.frame.size.height)]; 
} 
@end 

@implementation UIToolbar (CustomImage) 
- (void)drawRect:(CGRect)rect { 
    UIImage *image = [UIImage imageNamed: @"btmbar_Bg.png"]; 
    [image drawInRect:CGRectMake(0, 0, self.frame.size.width, self.frame.size.height)]; 
} 
@end 

回答

1

drawLayer或方法的drawRect不會被調用,直到你繼承UINavigationBar的,並調用drawRect中出現。

關注該碼 -

@implementation UINavigationBar (UINavigationBarCategory) 

- (void)drawLayer:(CALayer *)layer inContext:(CGContextRef)ctx 
{ 




     if([self isMemberOfClass:[UINavigationBar class]]) 
     { 
      UIImage *image; 


      image=[UIImage imageNamed:@"title_768.png"]; 



      CGContextClip(ctx); 
      CGContextTranslateCTM(ctx, 0, image.size.height); 
      CGContextScaleCTM(ctx, 1.0, -1.0); 
      CGContextDrawImage(ctx, 
           CGRectMake(0, 0, self.frame.size.width, self.frame.size.height), image.CGImage); 


     } 
     else 
     {   
      [super drawLayer:layer inContext:ctx];  
     } 

} 
@end 

並在代碼中添加MyNavigationBar.h和MyNavigationBar.m文件 -

MyNavigationBar.h -

#import <Foundation/Foundation.h> 

@interface MyNavigationBar : UINavigationBar <UINavigationBarDelegate> 

@end 

MyNavigationBar.m -

#import "MyNavigationBar.h" 

@implementation MyNavigationBar 

- (id)initWithFrame:(CGRect)frame { 
    if (self = [super initWithFrame:frame]) { 
     // Initialization code 
    } 
    return self; 
} 

- (void)drawRect:(CGRect)rect 
{ 



    UIImage *image; 

    image=[UIImage imageNamed:@"title_768.png"]; 


    [image drawInRect: CGRectMake(0, 0, self.frame.size.width, self.frame.size.height) ]; 


} 

- (void)navigationController:(UINavigationController *)navigationController willShowViewController:(UIViewController *)viewController animated:(BOOL)animated{ 

} 

- (void)navigationController:(UINavigationController *)navigationController didShowViewController:(UIViewController *)viewController animated:(BOOL)animated{ 
} 


@end 

現在使用MyNavigationBar子類化您的UINavigationBar。

0
在這種情況下,使用

驗證碼:

float version = [[[UIDevice currentDevice] systemVersion] floatValue]; 
    if (version >= 5.0) { 
     UINavigationBar *navBar = self.navigationController.navigationBar; 
     UIImage *image = [UIImage imageNamed:@"topbar.png"]; 
     [navBar setBackgroundImage:image forBarMetrics:UIBarStyleDefault]; 
    }