The CyanideScroller class is nice, I've just updated it a little bit. Above all I have made sure that the knob is drawn only if necessary. This is my class:
import AppKit
/**
A custom object that controls scrolling of a document view
within a scroll view or other type of container view.
*/
class CustomScroller: NSScroller {
/**
Depository of the horizontal Boolean value.
*/
private var _isHorizontal: Bool = false
/**
A Boolean value that indicates whether the scroller is horizontal.
*/
public var isHorizontal: Bool {
return _isHorizontal
}
/**
The background color.
*/
public var backgroundColor: NSColor = NSColor.black
/**
The knob color.
*/
public var knobColor: NSColor = NSColor.white
/**
A Boolean value that indicates whether the scroller draws its background.
*/
public var drawsBackground: Bool = true
override var frame: CGRect {
didSet {
let size = frame.size
_isHorizontal = size.width > size.height
}
}
override func draw(_ dirtyRect: NSRect) {
if drawsBackground {
backgroundColor.setFill()
dirtyRect.fill()
}
if (usableParts == .allScrollerParts) {
drawKnob()
}
}
/**
Draws the knob.
*/
override func drawKnob() {
knobColor.setFill()
let dx, dy: CGFloat
if _isHorizontal {
dx = 0
dy = 3
} else {
dx = 3
dy = 0
}
let frame = rect(for: .knob).insetBy(dx: dx, dy: dy)
let roundedPath = NSBezierPath(roundedRect: frame, xRadius: 3, yRadius: 3)
roundedPath.fill()
}
}