【问题标题】:Customize NavigationBar in iOS 5在 iOS 5 中自定义导航栏
【发布时间】:2011-12-09 06:01:33
【问题描述】:

iOs4我使用这个sn-p创建了一个自定义导航栏

#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创建一个自定义导航栏。

【问题讨论】:

    标签: iphone ipad ios4 uinavigationbar ios5


    【解决方案1】:

    您需要使用外观代理。但是,请确保检查是否为 iOS4 做出了响应。将您当前的方法保留在 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】:

      这里提到了同样的问题:UINavigationBar's drawRect is not called in iOS 5.0。请检查这可能对您有帮助。

      【讨论】:

        【解决方案3】:

        只需将以下代码放入应用程序委托中即可,即使在 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];
        

        【讨论】:

          【解决方案4】:

          查看iOS版本并设置导航栏图片

          if([[[UIDevice currentDevice] systemVersion]floatValue] [[UINavigationBar 外观] setBackgroundImage:[UIImage imageNamed:@"Sample.png"] forBarMetrics:UIBarMetricsDefault]; }

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2011-09-04
            • 2015-08-12
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多