Last active
June 29, 2017 19:11
-
-
Save soffes/613f6d0784a89060905b to your computer and use it in GitHub Desktop.
Handy utility view when working with UIStackView
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)) | |
} | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)) | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
will try it