Guest User

Untitled

a guest
Jan 19th, 2018
123
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.59 KB | None | 0 0
  1. public typealias Stack = UIStackView
  2.  
  3. public extension Stack {
  4. @nonobjc public convenience init(_ views: UIView..., with: (UIStackView) -> Void = { _ in }) {
  5. self.init(arrangedSubviews: views)
  6. with(self)
  7. }
  8.  
  9. @nonobjc public convenience init(_ views: [UIView], axis: UILayoutConstraintAxis = .horizontal, spacing: CGFloat = 0, alignment: UIStackViewAlignment = .fill, distribution: UIStackViewDistribution = .fill) {
  10. self.init(arrangedSubviews: views)
  11. self.axis = axis
  12. self.spacing = spacing
  13. self.alignment = alignment
  14. self.distribution = distribution
  15. }
  16.  
  17. @nonobjc public convenience init(style: ((UIStackView) -> Void)..., views: [UIView]) {
  18. self.init(arrangedSubviews: views)
  19. style.forEach { $0(self) }
  20. }
  21. }
  22.  
  23. public final class Spacer: UIView { // using `UIView` and not `UILayoutGuide` to support stack views
  24. @nonobjc public convenience init(width: CGFloat) { self.init(.width(width)) }
  25. @nonobjc public convenience init(minWidth: CGFloat) { self.init(.width(minWidth), isFlexible: true) }
  26. @nonobjc public convenience init(height: CGFloat) { self.init(.height(height)) }
  27. @nonobjc public convenience init(minHeight: CGFloat) { self.init(.height(minHeight), isFlexible: true) }
  28.  
  29. private enum Dimension {
  30. case width(CGFloat), height(CGFloat)
  31. }
  32.  
  33. private init(_ dimension: Dimension, isFlexible: Bool = false) {
  34. super.init(frame: .zero)
  35. Constraints(for: self) {
  36. switch dimension {
  37. case let .width(width):
  38. $0.width.set(width, relation: isFlexible ? .greaterThanOrEqual : .equal)
  39. if isFlexible { $0.width.set(width).priority = UILayoutPriority(42) } // disambiguate
  40. $0.height.set(0).priority = UILayoutPriority(42) // disambiguate
  41. case let .height(height):
  42. $0.height.set(height, relation: isFlexible ? .greaterThanOrEqual : .equal)
  43. if isFlexible { $0.height.set(height).priority = UILayoutPriority(42) } // disambiguate
  44. $0.width.set(0).priority = UILayoutPriority(42) // disambiguate
  45. }
  46. }
  47. }
  48.  
  49. public required init?(coder aDecoder: NSCoder) {
  50. super.init(coder: aDecoder)
  51. }
  52.  
  53. // don't draw anything
  54. override public class var layerClass: AnyClass { return CATransformLayer.self }
  55. override public var backgroundColor: UIColor? { get { return nil } set { return } }
  56. }
  57.  
  58. // MARK: Insets
  59.  
  60. public typealias Insets = UIEdgeInsets
  61. public extension UIEdgeInsets {
  62. public init(_ all: CGFloat) { self = UIEdgeInsetsMake(all, all, all, all) }
  63. }
Add Comment
Please, Sign In to add comment