I'm trying to draw a line in my MacOS app but I do not see the line. What can be a problem?
My code looks like:
func addLine() {
let path = NSBezierPath()
path.move(to: NSPoint(x: 100.0, y: 100))
path.line(to: NSPoint(x: 200.0, y: 200.0))
NSColor.green.setFill()
NSColor.green.setStroke()
path.close()
path.stroke()
}
And I call it in:
override func viewDidLoad() {
super.viewDidLoad()
addLine()
}
Am I doing something wrong? I just do not see anything in my window.
Have you created your own subclass of a
NSView?If I create a new view and add your code like so:
And I then - in a storyboard - drag a "Custom View" to the canvas, change the type of the view to be
MyViewlike soThen I see this when running the app:
If you prefer to add the view in code, you can do something like this:
So, your code seems to work, I'm just not sure how you are trying to use it.
Hope that gives you something to continue with.