79120730

Date: 2024-10-24 07:07:47
Score: 0.5
Natty:
Report link

This seems to be due to the List's layout behaviour. I don't know any solution other than not using a List in this case.

Here's some reproducible code where you can uncomment the List to see the difference.

import SwiftUI

struct ButtonContextMenuTest: View {
    
    var body: some View {
        // List {
            Section {
                ScrollView(.horizontal, showsIndicators: false) {
                    LazyHStack(spacing: 15) {
                        ForEach(0..<8) { index in
                            Button(action: {
                                //action here...
                            }) {
                                VStack(spacing: 10) {
                                    Image(systemName: "plus")
                                        .resizable()
                                        .aspectRatio(contentMode: .fit)
                                        .frame(width: 30, height: 30)
                                        .cornerRadius(10)
                                    
                                    Text("Button")
                                        .foregroundColor(.primary)
                                        .font(.footnote)
                                        .multilineTextAlignment(.center)
                                        .padding(.top, 5)
                                        .frame(width: 80)
                                }
                                .frame(width: 80, height: 100)
                                .background(.thinMaterial, in: RoundedRectangle(cornerRadius: 12))
                            }
                            .contextMenu {
                                Button("HELLO"){
                                }
                            }
                            
                        }
                    }
                    .padding()
                }
            }
        }
    // }
}

#Preview {
    ButtonContextMenuTest()
        .preferredColorScheme(.dark)
}

When not in a List, the contextMenu behaves as expected:

enter image description here

Reasons:
  • Blacklisted phrase (1.5): any solution
  • Long answer (-1):
  • Has code block (-0.5):
  • Low reputation (0.5):
Posted by: Andrei G.