Skip to content

Instantly share code, notes, and snippets.

@soffes
Last active June 29, 2017 19:11
Show Gist options
  • Save soffes/613f6d0784a89060905b to your computer and use it in GitHub Desktop.
Save soffes/613f6d0784a89060905b to your computer and use it in GitHub Desktop.
Handy utility view when working with UIStackView
import UIKit
/// Space view intented to be used with auto layout.
/// Similar to UIStackView, setting a background color is not supported.
final class SpaceView: UIView {
// MARK: - Properties
private let contentSize: CGSize
// MARK: - Initializers
init(size: CGSize) {
contentSize = size
super.init(frame: .zero)
}
convenience init(height: CGFloat) {
self.init(size: CGSize(width: UIViewNoIntrinsicMetric, height: height))
}
convenience init(width: CGFloat) {
self.init(size: CGSize(width: width, height: UIViewNoIntrinsicMetric))
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
// MARK: - UIView
override class var layerClass: AnyClass {
return CATransformLayer.self
}
override var intrinsicContentSize: CGSize {
return contentSize
}
}
extension UIStackView {
func addSpace(_ length: CGFloat = 0) {
switch axis {
case .horizontal: addArrangedSubview(SpaceView(width: length))
case .vertical: addArrangedSubview(SpaceView(height: length))
}
}
}
import AppKit
/// Space view intented to be used with auto layout.
final class SpaceView: NSView {
// MARK: - Properties
private let contentSize: CGSize
// MARK: - Initializers
init(size: CGSize) {
contentSize = size
super.init(frame: .zero)
}
convenience init(height: CGFloat) {
self.init(size: CGSize(width: NSViewNoIntrinsicMetric, height: height))
}
convenience init(width: CGFloat) {
self.init(size: CGSize(width: width, height: NSViewNoIntrinsicMetric))
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
// MARK: - NSView
override var intrinsicContentSize: CGSize {
return contentSize
}
}
extension NSStackView {
func addSpace(_ length: CGFloat = 0) {
switch orientation {
case .horizontal: addArrangedSubview(SpaceView(width: length))
case .vertical: addArrangedSubview(SpaceView(height: length))
}
}
}
@chandlervdw
Copy link

@soffes is this intended to be a parent of UIStackView which just fills the screen?

@vaderdan
Copy link

will try it

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment