I’m working on a macOs 13 app and I’m using the new NavigationSplitView
. The problem is that it doesn’t let us use the .onDeleteCommand(perform:)
(or maybe i’m using it wrong). Here is what I did :
In order to use the .onDeleteCommand(perform:)
, the view needs to be focused. I did a simple app showing 3 rectangles that I can select with the TAB key, and when I hit DELETE key or in the menu bar Edit > Delete (both trigger the .onDeleteCommand
), it switches to white or to its original colour.
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)
}
}
}
^^^ This works as it should ^^^
But if you put it in a NavigationSplitView
like this :
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)
}
}
}
}
If you press DELETE or Edit > Delete when a rectangle is focused as I explained, it doesn’t anything. In fact, the Edit > Delete isn’t clickable at all.
2
Answers
You need to use
onDeleteCommand
modifier directly on NavigationSplitView like this:With some input from https://swiftwithmajid.com/2021/03/03/focusedvalue-and-focusedbinding-property-wrappers-in-swiftui, I could solve this problem for me as follows:
This also works nicely for multiple views. Simply use the .focusedValue method instead of .onDeleteCommand. No further references or parameters required.