UPGRADE YOUR SKILLS: Learn advanced Swift and SwiftUI on Hacking with Swift+! >>

Dark material type with SwiftUI - .preferedColorScheme does not work

Forums > SwiftUI

Good morning,

I'd like to use the systemMaterialDark with the new SwiftUI Material but I only have dynamic options. I could use .colorScheme(.dark) - It works but this modifier is depreciated and replaced by .preferredColorScheme Unfortunately .preferredColorScheme(.dark) does not work :(

Do you have an idea to make it work without the depreciated .colorScheme ?

Thank you in advance for your help

3      

You have to use a UIViewRepresentable...

struct MaterialView: UIViewRepresentable {

    let material: UIBlurEffect.Style

    init(_ material: UIBlurEffect.Style) {
        self.material = material
    }

    func makeUIView(context: Context) -> UIVisualEffectView {
        UIVisualEffectView(effect: UIBlurEffect(style: material))
    }

    func updateUIView(_ uiView: UIVisualEffectView, context: Context) {
        uiView.effect = UIBlurEffect(style: material)
    }
}

struct ContentView: View {

    var body: some View {
        Text("Hello world")
            .foregroundColor(.white)
            .frame(width: 280, height: 100)
            .background(MaterialView(.systemMaterialDark))
            .clipShape(RoundedRectangle(cornerRadius: 16))
    }
}

3      

Thanks for the help,

I'll do it like this. Seems that Paul's VisualEffects package is not so depreciated by iOS15

++

3      

BUILD THE ULTIMATE PORTFOLIO APP Most Swift tutorials help you solve one specific problem, but in my Ultimate Portfolio App series I show you how to get all the best practices into a single app: architecture, testing, performance, accessibility, localization, project organization, and so much more, all while building a SwiftUI app that works on iOS, macOS and watchOS.

Get it on Hacking with Swift+

Sponsor Hacking with Swift and reach the world's largest Swift community!

Archived topic

This topic has been closed due to inactivity, so you can't reply. Please create a new topic if you need to.

All interactions here are governed by our code of conduct.

 
Unknown user

You are not logged in

Log in or create account
 

Link copied to your pasteboard.