【问题标题】:Could not set Image on Navigation bar only in "ios 5.0"仅在“ios 5.0”中无法在导航栏上设置图像
【发布时间】:2012-01-17 12:27:49
【问题描述】:

在我的通用应用中,

我正在导航栏上设置图像...使用 **

objective c category...on UINavigationBar

**

代码在 iphone 的 ios 5.0 中运行良好 *代码在 iPhone/iPad 的 ios 4.3 中运行良好*

*But not working in **ios 5.0 iPad***





- (void) drawRect:(CGRect)rect 
    {
    UIImage *image;

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

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

      [self setTintColor:[UIColor clearColor]];

      NSLog(@"Draw Rect");

    }

【问题讨论】:

    标签: objective-c ios xcode cocoa-touch ios5


    【解决方案1】:

    要在 iOS 5 中为导航栏设置背景图像,您可以使用以下代码

    if ([self.navigationController.navigationBar respondsToSelector:@selector( setBackgroundImage:forBarMetrics:)]){
    [self.navigationController.navigationBar setBackgroundImage:[UIImage imageNamed:@"Navigation.png"] forBarMetrics:UIBarMetricsDefault];
    }
    

    但请记住,这在 iOS 4 中不起作用。要使其在两者中都起作用,您还需要向 UINavigationBar 添加一个类别

    @implementation UINavigationBar (UINavigationBarCategory)
    - (void)drawRect:(CGRect)rect {
    UIImage *img = [UIImage imageNamed:@"Navigation.png"];
    [img drawInRect:rect];
    }
    

    【讨论】:

    【解决方案2】:

    如果你想在应用程序范围内设置后台,你可以使用 iOS5 的 UIAppearance。

    [[UINavigationBar appearance] setBackgroundImage:X forBarMetrics:UIBarMetricsDefault];
    [[UINavigationBar appearance] setBackgroundImage:X forBarMetrics:UIBarMetricsLandscape];
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-05-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多