Skip to content

Instantly share code, notes, and snippets.

@zenangst
Created February 21, 2018 17:45
Show Gist options
  • Save zenangst/aba335623b78c8db07a618a17f26d21c to your computer and use it in GitHub Desktop.
Save zenangst/aba335623b78c8db07a618a17f26d21c to your computer and use it in GitHub Desktop.
How frames work on macOS - Example 1
import Cocoa
class ContainerView: ColorView {}
class ColorView: NSView {
required init(frame: CGRect, color: NSColor) {
super.init(frame: frame)
wantsLayer = true
layer?.backgroundColor = color.cgColor
}
required init?(coder decoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
}
let containerFrame = CGRect(origin: .zero,
size: CGSize(width: 200, height: 200))
let containerView = ContainerView(frame: containerFrame,
color: NSColor.blue.withAlphaComponent(0.25))
let colorFrame = CGRect(origin: .zero,
size: CGSize(width: 50, height: 50))
let colorView = ColorView(frame: colorFrame,
color: NSColor.green.withAlphaComponent(0.5))
containerView.addSubview(colorView)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment