2017-07-17 110 views
6

更新斯威夫特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

回答

7

右鍵 - 當前的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