I have a Cocoa project that uses storyboards and auto layout. I have a scroll view as the content view for the view controller. I have a text view inside the scroll view that I want to be able to move around like a sticky note or index card.
I started by overriding the mouseDown and mouseDragged functions for NSTextView. To test if these functions were called, I had them change the text view's background color. I tested the app, and the background color changed when I clicked in the text view and dragged it.
My next step was to change the text view's position when I clicked in it. I wrote the following code:
override open func mouseDown(with event: NSEvent) {
setFrameOrigin(NSPoint(x: 0, y: 0))
needsDisplay = true
}
According to Apple's documentation, calling setFrameOrigin repositions a view inside its superview. In my code sample, clicking inside the text view should change its position to the lower left corner of the superview. But when I test the code and click in the text view, the position of the text view doesn't change. I tried different values for the frame origin, but none of them changed the text view's position. I tried setting the frame origin to the superview's origin and the window's origin, but the position of the text view did not change.
What do I have to do to change the position of a text view?