iOS 7.1 [UINavigationBar 外观] setBarTintColor 覆盖不起作用

标签 ios iphone objective-c uinavigationbar ios7.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)。

我删除了自定义类。

最佳答案

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

关于iOS 7.1 [UINavigationBar 外观] setBarTintColor 覆盖不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22321988/

相关文章:

ios - .NET Streamed PDF 无法在移动浏览器中打开

ios - 使用 Quartz 解析 PDF 文档

iphone - 当用户触摸 iPhone 中的 super View 时,如何实现关闭 subview ?

iphone - 提交后更改iTunes图稿

ios - 我怎样才能用 Swift 重写这个 block ?

objective-c - 使用反射的属性类型或类

ios - Swift UICollectionView 水平滚动不起作用

ios - MapBox iOS SDK 框架

iphone - 在构建表格时如何在 UITableViewController 中显示 View ?

ios - TableView 在查询后未填充 (PFQuery)