UIAppearance Swift 4

Eli*_*tle 17 ios uiappearance swift swift4 xcode9-beta

更新到Swift 4后,我收到编译器错误:

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

这是我的viewWillAppear自定义Tab Bar Controller子类中的方法,我正在设置项目文本的字体.

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

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

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

Jus*_*ney 33

对 - 当前的Swift 4转换工具(从Xcode 9 Beta 4开始)有点过时了.

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

例如,在Swift 3中,我有:

UITabBarItem.appearance().setTitleTextAttributes([NSForegroundColorAttributeName: UIColor.white], for: .selected)
Run Code Online (Sandbox Code Playgroud)

Xcode通过将其更改为"帮助"我:

UIAppearance.appearance().setTitleTextAttributes([NSAttributedStringKey.foregroundColor: UIColor.white], for: .selected)
Run Code Online (Sandbox Code Playgroud)

我能够通过半回复将这些错误安静到:

UITabBarItem.appearance().setTitleTextAttributes([NSAttributedStringKey.foregroundColor: UIColor.white], for: .selected)
Run Code Online (Sandbox Code Playgroud)