UIAppearance斯威夫特4

UIAppearance斯威夫特4

问题描述:

更新斯威夫特4后,我得到一个编译器错误:UIAppearance斯威夫特4

Static member 'appearance' cannot be used on protocol metatype 'UIAppearance.Protocol'

这是我在我的自定义标签栏控制器的子类viewWillAppear方法,我设置的项目文本的字体。

override func viewWillAppear(_ animated: Bool) { 
    super.viewWillAppear(animated) 

    // compiler error on line below 
    UIAppearance.appearance().setTitleTextAttributes([NSAttributedStringKey.font: font], for: UIControlState.normal) 
} 

我无法解决这个问题,任何指导将不胜感激,谢谢!

+0

该代码应该做什么?通常外观是为具体的UI类设置的,例如, 'UIBarItem.appearance()。setTitleTextAttributes ...' –

+0

我会在问题中提供更多的上下文。这是一个自定义选项卡栏控制器类,我正在更改栏项目的字体。 –

+0

您需要从UI类调用,而不是直接从'UIAppearance'调用。 – dimpiax

右键 - 当前的Swift 4转换工具(Xcode 9 Beta 4版本)稍微有些被带走。

我能够通过恢复UIAppearance转换代码,然后更新各个属性来快速解决问题。

例如,在斯威夫特3我:

UITabBarItem.appearance().setTitleTextAttributes([NSForegroundColorAttributeName: UIColor.white], for: .selected) 

Xcode的“帮助”我出去,改成:

UIAppearance.appearance().setTitleTextAttributes([NSAttributedStringKey.foregroundColor: UIColor.white], for: .selected) 

我能够通过半恢复到安静的错误,到:

UITabBarItem.appearance().setTitleTextAttributes([NSAttributedStringKey.foregroundColor: UIColor.white], for: .selected) 
+0

同样在这里。迁移者将类型更改为UIAppearance,而不是UITabBarItem,UIImageView等。 – Womble