0

I have a theme in my app utilising UIAppearance to set custom colors for buttons, switches, etc. It works fine with all of the ObjC and UIKit elements in my app.

I just added a SwiftUI view, and it almost works for that too.

I have the following code for UISwitch to set the color to red...

UISwitch.appearance().onTintColor = UIColor.red

My SwiftUI view seems to honor that appearance when it first loads, but then reverts to the default green if I toggle the switch.

This seems like a bug to me, but any thoughts would be appreciated. You can see it happening in the video here… https://www.loom.com/share/89159ec0db974fdaba5d4020d4848f98

Joe
  • 343
  • 2
  • 11
  • 1
    Does this answer your question? [Set Toggle color in SwiftUI](https://stackoverflow.com/questions/56479674/set-toggle-color-in-swiftui) – lorem ipsum May 01 '21 at 17:12
  • 1
    Keep in mind the **major** differences between UIKit and SwiftUI, along with how SwiftUI is *still* evolving. Your question actually doesn't address certain things. (Don't worry, I'm not voting to close or downvoting. But I *am* pointing out to you the there *is* a major (implied) difference between tagging this *both* [swift] and [swiftui] - I think you should remove the [swift] tag.) Second comment coming up.... –  May 01 '21 at 17:28
  • Here's the thing: Last year (not the year before when SwiftUI was first introduced) Xcode gave you the option to create a new SwiftUI project that has a "lifecycle" of "SwiftUI App". If these are your choices you do not even have Xcode create an `AppDelegate` or a `SceneDelegate` class!. Now, it's been years since I coded in ObjC, but that's something you will not see in anything using that language. So my question? Where are you setting this `UIAppearance` and why do you think SwiftUI - **NOT** Swift, please :-) - is the issue when it belongs to `UIKit`? –  May 01 '21 at 17:33
  • @dfd I removed the Swift tag My app is 12 years old. It's about 80% ObjC, 20% Swift, and 0.05% SwiftUI. There's no way I can rewrite the whole thing in SwiftUI, but SwiftUI does make it much easier to create controls. I'm dropping support for iOS12 in order to be able to make use of SwiftUI. The reason I think this is a bug.... The Toggle switch is initially coloured as per my UIAppearance declaration. Turning it off and on again it loses that color. Surely it should be one or the other. If there's a way to globally set all SwiftUI colors I'll do that. I just don't know what that is – Joe May 01 '21 at 17:37
  • THanks, it's just a pet peeve of mine, as I used to follow the SwiftUI tag and am surprised at the amount of noise tagging Swift adds to that tag. My real point was that I really think `UIAppearance`, being `UIKit`, is not likely working because of that. SwiftUI is something that works on a Watch that (used to or may not?) isn't WatchKit, and Macs that aren't using UIKit. The dup link looks promising to me. The code you posted looks like something I'd do for either a UIKit app or a UIViewControllerRepresentable. –  May 01 '21 at 17:45
  • @dfd To answer the question about why I think it's SwiftUI rather than UIKit... SwiftUI is the newer of the two. It appears that SwiftUI knows how to tint the switch based on UIAppearance since that's what it does to begin with. The fact that toggling the switch changes its appearance would suggest a bug in SwiftUI. I read other posts of people asking how to tint Toggles and UIAppearance was suggested. A later post I saw said it was broken in iOS14 beta 3. I'm really looking for a way, whatever it is, to have consistent colouring over the app - both UIKit parts and SwiftUI parts. – Joe May 01 '21 at 17:45
  • 1
    Just read both your comments after my posting my last one. In a word? OUCH! So you are working with (1) two languages, (2) two stacks - I treat SwiftUI that separate as UIKIt - and (3) betas? Wow. BTW, 14.5 went through 8 betas before they released it Monday and 4 days later a 14.6 beta came out. About 5 weeks before WWDC! I gave up on SwiftUI in September 2019 - yes, still in beta - because of how much UIKit and GLKit code I needed to port. It's matured since (some) but I could see where something this deep may need either a "pure" switch in SwiftUI or UIKit, whichever works best. –  May 01 '21 at 17:56
  • Yeah, it's a bit of a nightmare. SwiftUI is very promising for creating quick controls. If it wasn't for this little bug, I'd be quite happy with it. I'm not using betas - I just saw in someone else's post that the UIAppearance was broken. – Joe May 01 '21 at 18:18

0 Answers0