2011-10-17 228 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]; }