串列選擇不顯示如果我將 NavigationLink 和 .contextMenu 添加到串列中,當我選擇一行時,選擇就會消失。

struct ContentView: View {
@State private var selection: String?
let names = ["Cyril", "Lana", "Mallory", "Sterling"]
var body: some View {
NavigationView {
List(names, id: \.self, selection: $selection) { name in
NavigationLink(destination: Text("Hello, world!")) {
Text(name)
.contextMenu {
Button(action: {}) {
Text("Tap me!")
}
}
}
}
.toolbar {
EditButton()
}
}
}
}
uj5u.com熱心網友回復:
我們可以在編輯模式下構建時禁用背景關系選單按鈕(因為按鈕是問題的原因)。
這是一種可能的方法 - 需要重新設計來處理editMode內部背景關系選單(另請參見行內注釋)。
使用 Xcode 13.2 / iOS 15.2 測驗

struct ContentViewSelection: View {
@State private var selection: String?
let names = ["Cyril", "Lana", "Mallory", "Sterling"]
var body: some View {
NavigationView {
List(names, id: \.self, selection: $selection) { name in
// separated view is needed to use editMode
// environment value
NameCell(name: name)
}
.toolbar {
EditButton()
}
}
}
}
struct NameCell: View {
@Environment(\.editMode) var editMode // << !!
let name: String
var body: some View {
NavigationLink(destination: Text("Hello, world!")) {
Text(name)
}
.contextMenu {
if editMode?.wrappedValue == .inactive { // << !!
Button(action: {}) {
Text("Tap me!")
}
}
}
}
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/qianduan/405066.html
標籤:
