0

I'm attempt to change the tint color of all my toggles within my app to blue. I tried using both UISwitch.appearance().tintColor and UISwitch.appearance().onTintColor with no help.

import SwiftUI

@main
struct Studently: App {
        
    init() {
        UISwitch.appearance().tintColor = .systemBlue
        UISwitch.appearance().onTintColor = .systemBlue
    }
        
    var body: some Scene {
        WindowGroup {
            TabBar()
        }
    }
}

Image: .onTintColor and .tintColor does nothing in another view

The weird thing is, if the state property wrapper is true, the color will be applied. But once toggled to off then on, it returns back to standard green.

Image: Color only applied if the state property wrapped is set to true

I thought maybe this was just a human error on my end. So I tested this out more using UISwitch.appearance.thumbTintColor which applies to a toggle within another view.

@main

struct Studently: App {
        
    init() {
        UISwitch.appearance().tintColor = .systemBlue
        UISwitch.appearance().onTintColor = .systemBlue
        UISwitch.appearance().thumbTintColor = .red
    }
        
    var body: some Scene {
        WindowGroup {
            TabBar()
        }
    }
}

Image: .thumbTintColor changes the circle to red

.thumbTintColor is just for example that it can be applied, but neither .tintColor or .onTintColor are applied.

Of course, using .tint works when applied to a single toggle, but I want this to apply to all Toggles without using .tint on each one.

How can I achieve or fix this?

ARealChar
  • 11
  • 3
  • Are you using SwitchUI? Why aren't you using a Toggle component? https://developer.apple.com/documentation/swiftui/toggle Alternatively if you are using UIKit UISwift - try to assign the `onTintColor` and `thumbTintColor` on the instance - not through appearance - https://stackoverflow.com/questions/10348869/change-color-of-uiswitch-in-off-state – CloudBalancing Aug 24 '22 at 17:09
  • @CloudBalancing I am using SwiftUI. I was just unaware if UISwitch could work within SwiftUI as well – ARealChar Aug 24 '22 at 17:25
  • As such, use the Toggle component of SwiftUI - why mixing UIKit components is that mandatory? – CloudBalancing Aug 24 '22 at 20:30
  • @CloudBalancing no no no, not mandatory. I just thought UISwitch belonged with Toggle in UIKIt – ARealChar Aug 24 '22 at 23:33

0 Answers0