我正在开发一个macOs 13应用程序,我使用的是新的NavigationSplitView
。问题是它不允许我们使用.onDeleteCommand(perform:)
(或者我用错了)。下面是我所做的:
为了使用.onDeleteCommand(perform:)
,视图需要聚焦。我做了一个简单的应用程序,显示了3个矩形,我可以用TAB键选择,当我点击DELETE键或在菜单栏Edit〉Delete(两者都触发.onDeleteCommand
)时,它会切换到白色或原始颜色。
VStack {
Rectangle()
.fill((isColorDeleted.contains(.blue) ? Color.white : Color.blue))
.padding()
.focusable()
.focused($focusedColor, equals: .blue)
Rectangle()
.fill((isColorDeleted.contains(.red) ? Color.white : Color.red))
.padding()
.focusable()
.focused($focusedColor, equals: .red)
Rectangle()
.fill((isColorDeleted.contains(.yellow) ? Color.white : Color.yellow))
.padding()
.focusable()
.focused($focusedColor, equals: .yellow)
}
.onDeleteCommand {
if let focusedColor {
if !isColorDeleted.contains(focusedColor) {
isColorDeleted.append(focusedColor)
} else {
let idx = isColorDeleted.firstIndex(of: focusedColor)!
isColorDeleted.remove(at: idx)
}
}
}
^^^这就像它应该的那样工作^^^
但是如果你把它放在NavigationSplitView
中,就像这样:
NavigationSplitView(columnVisibility: $visibility) {
List {
Text("Main page")
}
} detail: {
VStack {
Rectangle()
.fill((isColorDeleted.contains(.blue) ? Color.white : Color.blue))
.padding()
.focusable()
.focused($focusedColor, equals: .blue)
Rectangle()
.fill((isColorDeleted.contains(.red) ? Color.white : Color.red))
.padding()
.focusable()
.focused($focusedColor, equals: .red)
Rectangle()
.fill((isColorDeleted.contains(.yellow) ? Color.white : Color.yellow))
.padding()
.focusable()
.focused($focusedColor, equals: .yellow)
}
.onDeleteCommand {
if let focusedColor {
if !isColorDeleted.contains(focusedColor) {
isColorDeleted.append(focusedColor)
} else {
let idx = isColorDeleted.firstIndex(of: focusedColor)!
isColorDeleted.remove(at: idx)
}
}
}
}
如果你按下DELETE或Edit〉Delete,当一个矩形被聚焦时,它没有任何作用。事实上,Edit〉Delete根本不能点击。
1条答案
按热度按时间u0sqgete1#
您需要直接在NavigationSplitView上使用
onDeleteCommand
修改器,如下所示: