Я пытаюсь добавить контекстное меню к кнопке внутри списка -> Раздел -> ScrollView (горизонтальный) в SwiftUI. Однако когда я нажимаю и удерживаю кнопку, контекстное меню применяется ко всему разделу, а не только к кнопке. Вот соответствующий код:
СКРИНШОТ 1
Почему контекстное меню применяется ко всему разделу, а не только к кнопке? Как сделать так, чтобы контекстное меню отображалось только для кнопки?
Section(header: Text("Templates")) {
ScrollView(.horizontal, showsIndicators: false) {
LazyHStack(spacing: 15) {
Button(action: {
// Action to add a new template
}) {
VStack {
Image(systemName: "plus")
.resizable()
.aspectRatio(contentMode: .fit)
.frame(width: 30, height: 30)
.padding()
.background(Color.white)
.cornerRadius(10)
Text("Create")
.foregroundColor(.primary)
.font(.footnote)
.multilineTextAlignment(.center)
.padding(.top, 5)
.frame(width: 80)
}
.frame(width: 80, height: 100)
}
.contextMenu {
Button("Hello") {
// Context menu action
}
}
}
}
}
private var templateSection: some View {
Section(header: Text("Templates")) {
ScrollView(.horizontal, showsIndicators: false) {
LazyHStack(spacing: 15) {
if templatesLimit > 0 {
Button(action: {
showingTemplateAddSheet = true
}) {
VStack {
Image(systemName: "plus")
.resizable()
.aspectRatio(contentMode: .fit)
.frame(width: 30, height: 30)
.padding()
.background(Color.white)
.cornerRadius(10)
Text("Create")
.foregroundColor(.primary)
.font(.footnote)
.multilineTextAlignment(.center)
.padding(.top, 5)
.frame(width: 80)
}
.frame(width: 80, height: 100)
}
.sheet(isPresented: $showingTemplateAddSheet) {
NewTemplateView(shouldRefreshParentView: $shouldRefreshParentView)
}
}
ForEach(filteredTemplates) { item in
Button(action: {
selectedTemplateItem = item
}) {
VStack {
Image(systemName: "\(item.iconName)")
.resizable()
.aspectRatio(contentMode: .fit)
.frame(width: 30, height: 30)
.padding()
.background(Color.white)
.cornerRadius(10)
Text(item.name)
.foregroundColor(.primary)
.font(.footnote)
.multilineTextAlignment(.center)
.padding(.top, 5)
.frame(width: 80)
}
.frame(width: 80, height: 100)
}
}
if viewModel.isLoadingTemplatesMore {
ForEach(0..
Подробнее здесь: https://stackoverflow.com/questions/791 ... n-in-swift