I'm writing a resizable view in SwiftUI on macOS. Drag its edge can change its size. I'd like to show according cursor when dragging different edges. So I looked up Apple NSCursor documentation, however, I cannot find resize cursors like those generally used in system as shown below.
How can I get those cursors? I attached my code below in which drag the Divider()
to resize the cyan rectangle. Hovering on that Divider()
will change the cursor.
struct ContentView: View {
@AppStorage("InspectorHeight") var inspectorHeight = 200.0
var body: some View {
ZStack(alignment: .top) {
Color.cyan
.frame(width: 100)
.frame(height: CGFloat(inspectorHeight))
Divider()
.padding(.vertical, 2)
.onHover { inside in
if inside {
NSCursor.resizeUpDown.push()
} else {
NSCursor.pop()
}
}
.gesture(
DragGesture()
.onChanged { drag in
inspectorHeight = max(100, inspectorHeight - Double(drag.translation.height))
}
)
}
.frame(height: 600, alignment: .bottom)
.padding()
}
}
The system cursors you're looking for don't exist, so you'll have to create custom ones, which you should be able to approximate using SFSymbols, e.g.:
NSCursor(image: NSImage(systemSymbolName: "arrow.up.and.down", accessibilityDescription: nil)!, hotSpot: NSPoint(x: 8, y: 8)).push()