NEW: My new book Pro SwiftUI is out now – level up your SwiftUI skills today! >>

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

1      

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))
    }
}

1      

Thanks for the help,

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

++

1      

Hacking with Swift is sponsored by Play

SPONSORED Play is the first native iOS design tool created for designers and engineers. You can install Play for iOS and iPad today and sign up to check out the Beta of our macOS app with SwiftUI code export. We're also hiring engineers!

Click to learn more about Play!

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.