SwiftUI ContextMenu:设置阴影形状或隐藏它

SwiftUI ContextMenu: Set shape of shadow or hide it

我想使用带有圆形按钮的上下文菜单。如果通过长按激活 ContextMenu,则按钮周围会出现一个圆角矩形的阴影。我已经尝试使用 .contentShape(Circle()),但根本不起作用。

有没有办法隐藏这个阴影或改变它的形状使其与按钮匹配?也许可以修改底层 UIKit 视图的属性?

感谢您的回答!

import SwiftUI

struct ContentView: View {
    var body: some View {
        Circle()
            .frame(width: 100, height: 100, alignment: .center)
            .foregroundColor(.yellow)
            .contentShape(Circle()) //does not work
            .contextMenu
        {
            Button(action: {}, label:
                    {
                Text("Action 1")
            })
            
            Button(action: {}, label:
                    {
                Text("Action 2")
            })
        }
    }
}

您需要为上下文菜单预览设置 contentMenu 形状。

您可以使用: .contentShape(.contextMenuPreview, Circle())

您可以在 Apple Developer Documentation

上阅读相关内容