在iOS 5中自定义NavigationBar

4
在iOs4中,我使用以下代码片段创建了一个自定义导航栏
#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个回答

4

你需要使用外观代理(Appearance Proxy)。但是,在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:...
}

3

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] setBackgroundImage:[UIImage imageNamed:@"Sample.png"] forBarMetrics:UIBarMetricsDefault]; }


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接