Home > Mobile >  SwiftUI ContextMenu: Set shape of shadow or hide it
SwiftUI ContextMenu: Set shape of shadow or hide it

Time:01-19

I want to use a ContextMenu with a round button. If the ContextMenu is activated by a long press, a shadow with the shape of a rounded rectangle surrounds the button. I already tried using .contentShape(Circle()), but that didn't work at all.

Is there a way to hide this shadow or to change the shape of it so it matches the button? Maybe the properties of the underlying UIKit view can be modified?

Thanks for answering!

enter image description here

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

CodePudding user response:

You need to set contentMenu shape for context menu preview.

You can do that using: .contentShape(.contextMenuPreview, Circle())

You can read about this on Apple Developer Documentation

  •  Tags:  
  • Related