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

contextMenu does not respect preferredColorScheme, how to hack it?

Forums > SwiftUI

struct ContentView: View {
    var body: some View {
        HStack {
            Text("Hello, world!")
                .padding()
                .contextMenu(ContextMenu(menuItems: {
                   Text("Menu Item 1")
                   Text("Menu Item 2")
                   Text("Menu Item 3")
                }))
        }
        .preferredColorScheme(.light)
    }
}

If you run this with dark mode turned on, the context menu will be in dark mode but the rest of the app will be in light mode.

I've run into this same problem for a few different components, for Alerts, I was able to fix the broken SwiftUI implementations like this:

UIView.appearance(whenContainedInInstancesOf: [UIAlertController.self]).overrideUserInterfaceStyle = isDark ? .dark : .light

However UIMenu does not implement the right protocols. It works if I use UIView, but that seems to break SwiftUI updating properly when the color scheme is changed in the app.

So, how to make the contextMenus appear correctly?

4      

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.