【问题标题】:iOS 7.1 [UINavigationBar appearance] setBarTintColor override not workingiOS 7.1 [UINavigationBar 外观] setBarTintColor 覆盖不起作用
【发布时间】:2014-03-13 16:36:26
【问题描述】:

为了以编程方式更改 barTint 颜色,我创建了一个自己的 UINavigationBar 类,它扩展了 UINavigationBar。在那里,我重写了 setBarTintColor 方法来更改颜色。在 iOS 7.1 中,这个方法永远不会被调用,所以我现在从“awakeFromNib”方法手动调用它,但我认为这是问题的开始。

我使用这个自定义类来覆盖以下外观设置:

[[UINavigationBar appearance] setBarTintColor:[UIColor colorWithWhite:0.302 alpha:1.000]];

在我的自定义类中使用此方法:

- (void)setBarTintColor:(UIColor *)barTintColor
{
    UIDevice *device = [UIDevice currentDevice];
    if(![NachtModusController NachtModus])
    {
        if (device.platformType == UIDevice4iPhone || device.platformType == UIDevice4SiPhone)
        {
            [super setBarTintColor:[UIColor colorWithWhite:1.000 alpha:1.000]];
        }
        else
        {
            [super setBarTintColor:[UIColor colorWithWhite:1.000 alpha:0.800]];
        }
    }
    else
    {
        //Nachtmodus.
        if (device.platformType == UIDevice4iPhone || device.platformType == UIDevice4SiPhone)
        {
            [super setBarTintColor:[UIColor colorWithWhite:0.302 alpha:1.000]];
        }
        else
        {
            [super setBarTintColor:[UIColor colorWithWhite:0.302 alpha:0.900]];
        }

    }
}

我发现:

- (void)setBackgroundImage:(UIImage *)backgroundImage forBarMetrics:(UIBarMetrics)barMetrics

在 7.1 中没有被调用,但是:

- (void)setBackgroundImage:(UIImage *)backgroundImage forBarPosition:(UIBarPosition)barPosition barMetrics:(UIBarMetrics)barMetrics

是。

如何使用自定义类覆盖 setBarTintColor 外观设置?

我的解决方案:

好像是这样设置图片的:

[[UINavigationBar appearance] setBackgroundImage:[UIImage imageNamed:@"navbar"] forBarMetrics:UIBarMetricsDefault];

然后像这样重置它:

[[UINavigationBar appearance] setBackgroundImage:nil forBarMetrics:UIBarMetricsDefault];

在 iOS7.1 中也造成了一些副作用(与 7.0 大不相同)

我删除了背景图片,我只使用了 barTint 颜色,我通过外观选项更改了它并更改了当前 (self.navigationController.navigationbar)。

我删除了我的自定义类。

【问题讨论】:

    标签: ios iphone objective-c uinavigationbar ios7.1


    【解决方案1】:

    您不应覆盖setBarTintColor: 以更改barTintColor。在您的情况下,您“破坏”了 setBarTintColor: 方法的功能,因为它忽略了输入参数。此外,只要您不调用 setBarTintColor: 函数,您的 NavigationBar 就不会按照您的意图更改颜色。
    您应该将此代码移动到创建导航栏时调用它的位置。在创建 NavigationBar 之后从外部调用它,或者覆盖 NavigationBar 类中的初始化方法。

    【讨论】:

    • 当我从三个“init”方法或 awakeFromNib 调用 setBarTintColor: 时,它不起作用。我应该使用什么方法?
    • 你真正想要实现什么?一个白色的NavigationBar?那么你不应该继承NavigationBar,而是从viewController(可能是viewDidLoad方法)调用[self.navigationController.navigationBar setBarTintColor:[UIColor colorWithWhite:1.000 alpha:1.000]],它位于NavigationController内部。
    • 我想要一种简单的方法来根据一天中的当前时间设置导航栏的样式,并且始终保持相同的方式,而无需冗余代码。例如,夜间为黑色,白天为蓝色。我通过在 Interfacebuilder 中设置自定义类很容易实现这一点,在 iOS7.0 中我可以覆盖外观设置。
    • 在覆盖 setTintColor 和 setBackgroundImage 以创建不褪色但具有特定 alpha 的标题颜色时遇到相同的问题,还可以在某些重复背景而不是重复背景的视图上创建背景图像层只是拉伸图像。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-11-21
    • 1970-01-01
    • 2013-12-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多