【问题标题】:Navigation Bar TintColor not working with UIColor initWithPatternImage iOS5导航栏 TintColor 不适用于 UIColor initWithPatternImage iOS5
【发布时间】:2012-03-07 05:45:56
【问题描述】:

我正在使用 iOS5,我正在尝试为我的导航栏设置自定义 TintColor。

代码:这是我在 viewWillAppear 中使用的:

[self.navigationController.navigationBar setTintColor:[UIColor colorWithPatternImage:[UIImage imageNamed:@"NavigationBarImage.png"]]];

图像是 320 x 44 PNG 图像,可在我的应用程序中使用。

当我使用时

self.navigationController.navigationBar.tintColor = [UIColor orangeColor];

它显示橙色,但不适用于PatternImage。 我做错了什么?

【问题讨论】:

    标签: ios5 uinavigationbar uicolor tintcolor


    【解决方案1】:

    您可以使用 UINavigationBar 类别来设置背景图片。

    #import "UINavigationBar+Background.h"
    
    @implementation UINavigationBar (Background)
    
    - (void) drawRect:(CGRect)rect {
        UIImage *image = [UIImage imageNamed:@"NavigationBarImage.png"];
        [image drawInRect:CGRectMake(0, 0, self.frame.size.width, self.frame.size.height)];
    }
    
    @end
    

    【讨论】:

    • 感谢 fannheyward,实际上还有更简单的方法可以完成这项工作。我刚刚想通了。 [self.navigationController.navigationBar setBackgroundImage:[UIImage imageNamed:@"NavigationBarImage.png"] forBarMetrics:UIBarMetricsDefault];这适用于任何类型的定制图像。无需添加类别。
    • @ShriniketSarkar 我的错。为什么不直接使用[self.navigationController.navigationBar setBackgroundImage:[UIImage imageNamed:@"image.png"] forBarMetrics:UIBarMetricsDefault];?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-07-27
    • 1970-01-01
    • 2016-05-28
    • 2020-07-04
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多