Last active
January 10, 2018 18:12
-
-
Save kean/e77bac3625124b1de559a241a72d1e09 to your computer and use it in GitHub Desktop.
Stacks and Spacers
This file contains 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
public typealias Stack = UIStackView | |
public extension Stack { | |
@nonobjc public convenience init(_ views: UIView..., with: (UIStackView) -> Void = { _ in }) { | |
self.init(arrangedSubviews: views) | |
with(self) | |
} | |
@nonobjc public convenience init(_ views: [UIView], axis: UILayoutConstraintAxis = .horizontal, spacing: CGFloat = 0, alignment: UIStackViewAlignment = .fill, distribution: UIStackViewDistribution = .fill) { | |
self.init(arrangedSubviews: views) | |
self.axis = axis | |
self.spacing = spacing | |
self.alignment = alignment | |
self.distribution = distribution | |
} | |
@nonobjc public convenience init(style: ((UIStackView) -> Void)..., views: [UIView]) { | |
self.init(arrangedSubviews: views) | |
style.forEach { $0(self) } | |
} | |
} | |
public final class Spacer: UIView { // using `UIView` and not `UILayoutGuide` to support stack views | |
@nonobjc public convenience init(width: CGFloat) { self.init(.width(width)) } | |
@nonobjc public convenience init(minWidth: CGFloat) { self.init(.width(minWidth), isFlexible: true) } | |
@nonobjc public convenience init(height: CGFloat) { self.init(.height(height)) } | |
@nonobjc public convenience init(minHeight: CGFloat) { self.init(.height(minHeight), isFlexible: true) } | |
private enum Dimension { | |
case width(CGFloat), height(CGFloat) | |
} | |
private init(_ dimension: Dimension, isFlexible: Bool = false) { | |
super.init(frame: .zero) | |
Constraints(for: self) { | |
switch dimension { | |
case let .width(width): | |
$0.width.set(width, relation: isFlexible ? .greaterThanOrEqual : .equal) | |
if isFlexible { $0.width.set(width).priority = UILayoutPriority(42) } // disambiguate | |
$0.height.set(0).priority = UILayoutPriority(42) // disambiguate | |
case let .height(height): | |
$0.height.set(height, relation: isFlexible ? .greaterThanOrEqual : .equal) | |
if isFlexible { $0.height.set(height).priority = UILayoutPriority(42) } // disambiguate | |
$0.width.set(0).priority = UILayoutPriority(42) // disambiguate | |
} | |
} | |
} | |
public required init?(coder aDecoder: NSCoder) { | |
super.init(coder: aDecoder) | |
} | |
// don't draw anything | |
override public class var layerClass: AnyClass { return CATransformLayer.self } | |
override public var backgroundColor: UIColor? { get { return nil } set { return } } | |
} | |
// MARK: Insets | |
public typealias Insets = UIEdgeInsets | |
public extension UIEdgeInsets { | |
public init(_ all: CGFloat) { self = UIEdgeInsetsMake(all, all, all, all) } | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment