Reputation: 551
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.
Upvotes: 1
Views: 853
Reputation: 6876
Have you created your own subclass of a NSView
?
If I create a new view and add your code like so:
import Cocoa
class MyView: NSView {
override func draw(_ dirtyRect: NSRect) {
super.draw(dirtyRect)
addLine()
}
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 then - in a storyboard - drag a "Custom View" to the canvas, change the type of the view to be MyView
like so
Then I see this when running the app:
If you prefer to add the view in code, you can do something like this:
class ViewController: NSViewController {
override func viewDidLoad() {
super.viewDidLoad()
let myView = MyView()
myView.translatesAutoresizingMaskIntoConstraints = false
view.addSubview(myView)
myView.topAnchor.constraint(equalTo: view.topAnchor).isActive = true
myView.bottomAnchor.constraint(equalTo: view.bottomAnchor).isActive = true
myView.leftAnchor.constraint(equalTo: view.leftAnchor).isActive = true
myView.rightAnchor.constraint(equalTo: view.rightAnchor).isActive = true
}
}
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.
Upvotes: 2