Skip to content

Instantly share code, notes, and snippets.

@zenangst
Created February 21, 2018 17:59
Show Gist options
  • Save zenangst/0e0613a0ff936df76377b920080b74c7 to your computer and use it in GitHub Desktop.
Save zenangst/0e0613a0ff936df76377b920080b74c7 to your computer and use it in GitHub Desktop.
How frames work on macOS - Example 3
import Cocoa
class ContainerView: ColorView {
override var isFlipped: Bool { return true }
}
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: 150, height: 150))
let colorView = ColorView(frame: colorFrame,
color: NSColor.green.withAlphaComponent(0.5))
let colorSubviewFrame = CGRect(origin: .zero,
size: CGSize(width: 50, height: 50))
let colorSubview = ColorView(frame: colorSubviewFrame,
color: NSColor.yellow.withAlphaComponent(0.5))
containerView.addSubview(colorView)
colorView.addSubview(colorSubview)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment