Home > Mobile >  Resize cursors are missing in `NSCursor`
Resize cursors are missing in `NSCursor`

Time:02-03

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 enter image description here

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()
    }
}

CodePudding user response:

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()
  • Related