Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I add padding to a SwiftUI View's context menu preview?

I have a LazyVGrid with items in it, I don't have any padding on the items because I prefer to let the layout of the LazyVGrid manage the spacing between cells. but when I open the context menu on the item it displays the item as it is with no padding.

enter image description here

how can I tell the context menu to show the preview with added padding around the cell?

like image 428
Halpo Avatar asked Oct 16 '25 07:10

Halpo


1 Answers

In iOS 16 and higher, the contextMenu modifier takes a preview argument that is of type some View.

This view could have padding, or a customised Image. To get padding to work, you might need to wrap the views and padding in a parent view at the top of the hierarchy.

You haven’t shared your code so I can’t give you an example in your context, but it could look like this:

struct MyView: View {
    var body: some View {
        Text("Example")
            .contextMenu {
                // Context menu stuff
            } preview: {
                // Preview here
            }
    }
}
like image 162
Chris Avatar answered Oct 19 '25 12:10

Chris



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!