简体   繁体   中英

SwiftUI contextMenu only in dark mode

Does anyone know why the contextMenu will only appear in dark mode when you put it in the view of a button:

struct ContentView: View {
    var body: some View {
        Button(action: {
        
        }) {
            Text("Button with context menu")
                .contextMenu {
                    Text("Menu Item")
                }
        }
    }
}

Tried this on iOS and iPadOS, in the simulator and on device with os versions 14.4.1, 14.4.2 & 14.5.1. I'm also almost sure, that this wasn't always the case.

Probably a bug as @jnpdx said . However, if you put the contextMenu outside the Button, it works fine.

struct ContentView: View {
    var body: some View {
        Button(action: {
            print("Button pressed")
        }) {
            Text("Button with context menu")
        }
        /// outside
        .contextMenu {
            Button(action: {
                print("Menu button pressed")
            }) {
                Text("Menu Item")
            }
        }
    }
}

Result:

Light mode Dark mode
灯光模式 - 点击按钮,然后按住并点击菜单按钮 暗模式 - 点击按钮,然后按住并点击菜单按钮

iOS 16

This Bug is fixed in iOS 16 Beta (20A5283p)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM