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      

Hacking with Swift is sponsored by RevenueCat

SPONSORED Take the pain out of configuring and testing your paywalls. RevenueCat's Paywalls allow you to remotely configure your entire paywall view without any code changes or app updates.

Learn more here

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.