How to change tintAdjustmentMode in SwiftUI macOS

167 Views Asked by At

In my macOS SwiftUI app i have a toggle which looks fine, having a tint color

        Toggle(isOn: userManagedIsActive) {
            EmptyView()
        }
        .disabled(!canActivate)
        .toggleStyle(.switch)
        .overlay {
            if canActivate {
                Capsule(style: .continuous)
                    .stroke(toggleColor())
            }
        }
        .tint(toggleColor())

enter image description here enter image description here

However when the window goes inactive, the switch color changes (on left i opened Finder)

enter image description here

  • in iOS UIKit (even not macOS AppKit), this could have been controlled by tintAdjustmentMode but looks like there is no such option in SwiftUI

how to make the toggle not change its tin, or option 2, how could i dimm the selector and overlay as well? to matched the .dimmed gray look?

1

There are 1 best solutions below

1
Anbalagan D On
struct CustomToggleStyle: ToggleStyle {
    var onColor: Color = .blue
        
    func makeBody(configuration: Configuration) -> some View {
        HStack {
            RoundedRectangle(cornerRadius: 12.5)
                .fill(configuration.isOn ? onColor : Color.gray)
                .overlay {
                   Circle()
                       .fill(.white)
                       .padding(3)
                       .offset(x: configuration.isOn ? 10 : -10)
                    }
                    .frame(width: 42, height: 25)
                    .onTapGesture {
                        withAnimation(.spring) {
                            configuration.isOn.toggle()
                        }
                    }
            }
        }
    }

Use above 'toggleStyle' like below

Toggle(isOn: userManagedIsActive) {
    EmptyView()
}
.toggleStyle(CustomToggleStyle(onColor: .purple))