2011-10-17 186 views
4

iOs4我使用這個片段自定義導航欄在iOS 5中

#import "UINavigationBar+CustomImage.h" 


@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 

創建一個自定義導航欄,這是很好的在我的應用程序的iOS4。現在我想在iOs5中運行這個問題,問題是自定義導航欄沒有以我想要的方式顯示。

任何人都可以幫助我創建一個自定義導航欄iOs5

回答

4

您需要使用外觀代理。但是,請確保檢查iOS4的respondsToSelector。爲iOS4保留當前的方法,它將同時適用於這兩種方法。

// not supported on iOS4 
UINavigationBar *navBar = [purchaseNavController navigationBar]; 
if ([navBar respondsToSelector:@selector(setBackgroundImage:forBarMetrics:)]) 
{ 
    // set globablly for all UINavBars 
    [[UINavigationBar appearance] setBackgroundImage:[UIImage imageNamed:@"brnlthr_nav.jpg"] forBarMetrics:UIBarMetricsDefault]; 

    // could optionally set for just this navBar 
    //[navBar setBackgroundImage:... 
} 
2

只要把下面的代碼在應用程序委託,它會正常工作,即使在iOS5中
我把它放在

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions 


// Create image for navigation background - portrait 

UIImage *NavigationPortraitBackground = [[UIImage imageNamed:@"NavigationPortraitBackground"] 
              resizableImageWithCapInsets:UIEdgeInsetsMake(0, 0, 0, 0)]; 

// Create image for navigation background - landscape 

UIImage *NavigationLandscapeBackground = [[UIImage imageNamed:@"NavigationLandscapeBackground"] 
              resizableImageWithCapInsets:UIEdgeInsetsMake(0, 0, 0, 0)]; 


// Set the background image all UINavigationBars 

[[UINavigationBar appearance] setBackgroundImage:NavigationPortraitBackground 
            forBarMetrics:UIBarMetricsDefault]; 
[[UINavigationBar appearance] setBackgroundImage:NavigationLandscapeBackground 
            forBarMetrics:UIBarMetricsLandscapePhone]; 
0

檢查的iOS版本,並設置導航欄圖像

if([[[[UIDevice currentDevice] systemVersion] floatValue] < 5.0){} else {
[[UINavigationBar appearance] setBackgroundIm年齡:[UIImage imageNamed:@「Sample.png」] forBarMetrics:UIBarMetricsDefault]; }