1

为了以编程方式更改 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)。

我删除了我的自定义类。

4

1 回答 1

1

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

于 2014-03-11T10:09:21.630 回答