UIAppearance Swift 4
Asked Answered
C

1

17

After updating to Swift 4, I am getting a compiler error:

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

Here is my viewWillAppear method in my custom Tab Bar Controller subclass, I am setting the font of the item text.

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

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

I'm having trouble fixing this, any guidance would be appreciated, thanks!

Chelsea answered 17/7, 2017 at 17:40 Comment(6)
What is that code supposed to do? Usually the appearance is set for a concrete UI class, e.g. UIBarItem.appearance().setTitleTextAttributes ...Corfu
I will provide more context in the question. This is a custom Tab bar controller class, I am changing the font of the bar items.Chelsea
You need to call from UI class, not directly from UIAppearance.Legator
It turned out that when updating code to Swift 4 from 3, Apple's helper introduced incorrect code.Chelsea
You should report it at bugs.swift.org so nobody else has to deal with it :)Liaison
same ... also having the same problemProminent
O
33

Right - the current Swift 4 conversion tool (as of Xcode 9 Beta 4) gets a little carried away.

I was able to fix the problem quickly by reverting the UIAppearance conversion code, then updating the individual attributes.

For example, in Swift 3 I had:

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

Xcode "helped" me out by changing it to:

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

I was able to quiet the errors by half-reverting, to:

UITabBarItem.appearance().setTitleTextAttributes([NSAttributedStringKey.foregroundColor: UIColor.white], for: .selected)
Ontologism answered 25/7, 2017 at 21:35 Comment(2)
Same here. The migrator is changing the type to UIAppearance, instead of, say, UITabBarItem, UIImageView etc.Circumfuse
you can even omit the NSAttributedStringKey, swift can infer itTizes

© 2022 - 2024 — McMap. All rights reserved.