Search code examples
swiftuiios-darkmode

SwiftUI: Force View to use light or dark mode


Is it possible in SwiftUI to force a View to use light or dark mode — like overrideUserInterfaceStyle in UIKit does?


Solution

  • .colorScheme() is deprecated does not work with the background well.

    .preferredColorScheme() is the way now. Does the job with the background too.

    TestView1()
       .preferredColorScheme(.dark)
    
    TestView2()
       .preferredColorScheme(.light)
    

    Developer docs for .preferredColorScheme()