I made a simple headerView that draws a grey bottom line above the main NSView, like the header in Xcode. Code is shown below:
import Cocoa
class HeaderView: NSView {
var border = CALayer()
required init?(coder decoder: NSCoder) {
super.init(coder: decoder)
self.wantsLayer = true
self.layer?.backgroundColor = NSColor.white.cgColor
border.backgroundColor = NSColor.quaternaryLabelColor.cgColor
layer?.addSublayer(border)
}
override func draw(_ dirtyRect: NSRect) {
super.draw(dirtyRect)
border.frame = CGRect(x: 0.0, y: 0.0, width: frame.width, height: CGFloat(1.0))
}
}
The line draws, but when I resize the window there is a small delay when the line is redrawn. So if I pull the right side of the window to the right, you can briefly see a gap in the line on the right side.
I also tried adding:
self.layerContentsRedrawPolicy = .duringViewResize
But that did not make a difference.
How can I make sure the line is constantly updated during resizing the window?
I fixed it by using an NSBezierPath
:
override func draw(_ dirtyRect: NSRect) {
super.draw(dirtyRect)
let path = NSBezierPath()
path.move(to: NSPoint(x: 0.0, y: 0.0))
path.line(to: NSPoint(x: frame.width, y: 0.0))
NSColor.quaternaryLabelColor.setStroke()
path.stroke()
}