WWDC24 SALE: Save 50% on all my Swift books and bundles! >>

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 Superwall.

SPONSORED Superwall lets you build & test paywalls without shipping updates. Run experiments, offer sales, segment users, update locked features and more at the click of button. Best part? It's FREE for up to 250 conversions / mo and the Superwall team builds out 100% custom paywalls – free of charge.

Learn More

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.