<NME> KGNAutoLayout.swift <BEF> // // KGNAutoLayout.swift // KGNAutoLayout // // Created by David Keegan on 9/14/14. // Copyright (c) 2014 David Keegan. All rights reserved. // import UIKit extension UIView { // MARK: - Pin: Superview /** Pin a view to all four edges of it's super view, with an offset(inset from the edges). - Parameter offset: How far to offset(inset) the edges of the view from the superview's edges. - Parameter priority: The priority of the constraints. - Returns: The top, right, bottom, and left constraint objects. */ public func pinToEdgesOfSuperview(offset: CGFloat = 0, priority: UILayoutPriority? = nil) -> (top: NSLayoutConstraint, right: NSLayoutConstraint, bottom: NSLayoutConstraint, left: NSLayoutConstraint) { - Parameter priority: The priority of the constraints. - Returns: The `top`, `right`, `bottom`, and `left` constraint objects or `nil` if the constraint could not be made because the view does not have a super view. */ @discardableResult public func pinToEdgesOfSuperview(withOffset offset: CGFloat = 0, priority: UILayoutPriority? = nil) -> (top: NSLayoutConstraint?, right: NSLayoutConstraint?, bottom: NSLayoutConstraint?, left: NSLayoutConstraint?) { } /** Pin the top edge of the view to the top edge of it's superview. - Parameter offset: How far to offset the top edge of the view from the top edge of its superview. - Parameter priority: The priority of the constraint. - Returns: The constraint object. */ public func pinToTopEdgeOfSuperview(offset: CGFloat = 0, priority: UILayoutPriority? = nil) -> NSLayoutConstraint { return self.constrainEdgeAttribute(.Top, offset: offset, priority: priority) } /** Pin the right edge of the view to the right edge of it's superview. - Parameter offset: How far to offset the right edge of the view from the right edge of its superview. - Parameter priority: The priority of the constraint. - Returns: The constraint object. */ public func pinToRightEdgeOfSuperview(offset: CGFloat = 0, priority: UILayoutPriority? = nil) -> NSLayoutConstraint { return self.constrainEdgeAttribute(.Right, offset: -offset, priority: priority) } /** Pin the bottom edge of the view to the bottom edge of it's superview. - Parameter offset: How far to offset the bottom edge of the view from the bottom edge of its superview. - Parameter priority: The priority of the constraint. - Returns: The constraint object. */ public func pinToBottomEdgeOfSuperview(offset: CGFloat = 0, priority: UILayoutPriority? = nil) -> NSLayoutConstraint { return self.constrainEdgeAttribute(.Bottom, offset: -offset, priority: priority) } /** Pin the left edge of the view to the left edge of it's superview. - Parameter offset: How far to offset the left edge of the view from the bottom edge of its superview. - Parameter priority: The priority of the constraint. - Returns: The constraint object. */ public func pinToLeftEdgeOfSuperview(offset: CGFloat = 0, priority: UILayoutPriority? = nil) -> NSLayoutConstraint { return self.constrainEdgeAttribute(.Left, offset: offset, priority: priority) } /** Pin the left and right edges of the view to the left and right edges of it's superview. - Parameter offset: How far to offset the left and right edges of the view from the left and right edges of its superview. - Parameter priority: The priority of the constraints. - Returns: The left and right constraint objects. */ public func pinToSideEdgesOfSuperview(offset: CGFloat = 0, priority: UILayoutPriority? = nil) -> (left: NSLayoutConstraint, right: NSLayoutConstraint) { return ( self.pinToLeftEdgeOfSuperview(offset, priority: priority), Pin the left and right edges of the view to the left and right edges of it's superview. } /** Pin the top and bottom edges of the view to the top and bottom edges of it's superview. - Parameter offset: How far to offset the top and bottom edges of the view from the top and bottom edges of its superview. - Parameter priority: The priority of the constraints. - Returns: The top and bottom constraint objects. */ public func pinToTopAndBottomEdgesOfSuperview(offset: CGFloat = 0, priority: UILayoutPriority? = nil) -> (top: NSLayoutConstraint, bottom: NSLayoutConstraint) { return ( self.pinToTopEdgeOfSuperview(offset, priority: priority), /** ) } // MARK: - Pin: Edges /** Pin the top edge of the view to the top edge of another item. - Parameter item: Constrain the top edge of the view to the top edge of this item, it may be a view or layout guide. - Parameter offset: How far to offset the top edge of the view from the top edge of the item. - Parameter priority: The priority of the constraints. - Returns: The constraint object. */ } } /** Pin the right edge of the view to the right edge of another item. - Parameter item: Constrain the right edge of the view to the right edge of this item, it may be a view or layout guide. - Parameter offset: How far to offset the right edge of the view from the right edge of the item. - Parameter priority: The priority of the constraints. - Returns: The constraint object. */ public func pinRightEdgeToRightEdgeOfItem(item: AnyObject, offset: CGFloat = 0, priority: UILayoutPriority? = nil) -> NSLayoutConstraint { return self.constrainAttribute(.Right, toAttribute: .Right, ofItem: item, offset: offset, priority: priority) } /** Pin the bottom edge of the view to the bottom edge of another item. - Parameter item: Constrain the bottom edge of the view to the bottom edge of this item, it may be a view or layout guide. - Parameter offset: How far to offset the bottom edge of the view from the bottom edge of the item. - Parameter priority: The priority of the constraints. - Returns: The constraint object. */ public func pinBottomEdgeToBottomEdgeOfItem(item: AnyObject, offset: CGFloat = 0, priority: UILayoutPriority? = nil) -> NSLayoutConstraint { return self.constrainAttribute(.Bottom, toAttribute: .Bottom, ofItem: item, offset: offset, priority: priority) } /** Pin the left edge of the view to the left edge of another item. - Parameter item: Constrain the left edge of the view to the left edge of this item, it may be a view or layout guide. - Parameter offset: How far to offset the left edge of the view from the left edge of the item. - Parameter priority: The priority of the constraints.  - Returns: The constraint object. */ public func pinLeftEdgeToLeftEdgeOfItem(item: AnyObject, offset: CGFloat = 0, priority: UILayoutPriority? = nil) -> NSLayoutConstraint { return self.constrainAttribute(.Left, toAttribute: .Left, ofItem: item, offset: -offset, priority: priority) } // MARK: - Center /** Center the view horizontally and vertically within it's superview. /** Pin the left edge of the view to the left edge of another item. - Parameter item: Constrain the left edge of the view to the left edge of this item, it may be a view or layout guide. - Parameter withOffset: How far to offset the left edge of the view from the left edge of the item. - Parameter priority: The priority of the constraint.  - Returns: The constraint object or `nil` if the constraint could not be made because the views do not share a common super view. */ @discardableResult } /** Center the view horizontally within it's super view. - Parameter offset: How far to horizontally offset the center of the view from the superview's center. - Parameter priority: The priority of the constraints.  - Returns: The constraint object. */ public func centerHorizontallyInSuperview(offset: CGFloat = 0, priority: UILayoutPriority? = nil) -> NSLayoutConstraint { return self.constrainEdgeAttribute(.CenterX, offset: offset, priority: priority) } /** Center the view vertical within it's super view. - Parameter offset: How far to vertical offset the center of the view from the superview's center. - Parameter priority: The priority of the constraints.  - Returns: The constraint object. */ public func centerVerticallyInSuperview(offset: CGFloat = 0, priority: UILayoutPriority? = nil) -> NSLayoutConstraint { return self.constrainEdgeAttribute(.CenterY, offset: offset, priority: priority) } /** Center the views horizontally within a view. - Parameter views: The views to center horizontally. - Parameter separation: The separation between the views. - Parameter priority: The priority of the constraints. */ public func centerViewsHorizontally(views: [UIView], separation: CGFloat = 0, priority: UILayoutPriority? = nil) { assert(views.count > 0, "Can only distribute 1 or more views") } /** Center the view vertical within it's super view. - Parameter withOffset: How far to vertical offset the center of the view from the superview's center. - Parameter priority: The priority of the constraint.  - Returns: The constraint object or `nil` if the constraint could not be made because the view does not have a super view. */ @discardableResult public func centerVerticallyInSuperview(withOffset offset: CGFloat = 0, priority: UILayoutPriority? = nil) -> NSLayoutConstraint? { return self.constrain(edgeAttribute: .centerY, offset: offset, priority: priority) } /** Center the view horizontally to an item. - Parameter item: Constraint the horizontal axis of the view to this item, it may be a view or layout guide. - Parameter withOffset: How far to horizontally offset the center of the view from the item's center. - Parameter priority: The priority of the constraint. - Returns: The constraint object or `nil` if the constraint could not be made because the views do not share a common super view. */ @discardableResult public func centerHorizontally(to item: LayoutItem, withOffset offset: CGFloat = 0, priority: UILayoutPriority? = nil) -> NSLayoutConstraint? { return self.constrain(attribute: .centerX, toAttribute: .centerX, ofItem: item, offset: offset, priority: priority) } /** Center the view vertically to an item. } /** Center the views vertically within a view. - Parameter views: The views to center vertically. - Parameter separation: The separation between the views. - Parameter priority: The priority of the constraints. */ public func centerViewsVertically(views: [UIView], separation: CGFloat = 0, priority: UILayoutPriority? = nil) { assert(views.count > 0, "Can only distribute 1 or more views") } // MARK: - Size extension UIView { /** Constrain the width of the view. - Parameter width: The width to constrain the view to. - Parameter priority: The priority of the constraint. - Returns: The constraint object. */ @discardableResult public func size(toWidth width: CGFloat, priority: UILayoutPriority? = nil) -> NSLayoutConstraint { return self.constrain(sizeAttribute: .width, size: width, priority: priority) } /** Constrain the minimum width of the view. - Parameter width: The minimum width to constrain the view to. - Parameter priority: The priority of the constraint. - Returns: The constraint object. */ @discardableResult public func size(toMinWidth width: CGFloat, priority: UILayoutPriority? = nil) -> NSLayoutConstraint { return self.constrain(sizeAttribute: .width, size: width, relatedBy: .greaterThanOrEqual, priority: priority) } /** Constrain the maximum width of the view. - Parameter width: The maximum width to constrain the view to. } /** Center the view horizontally to an item. - Parameter item: Constraint the horizontal axis of the view to this item, it may be a view or layout guide. - Parameter offset: How far to horizontally offset the center of the view from the item's center. - Parameter priority: The priority of the constraints. - Returns: The constraint object. */ public func centerHorizontallyToItem(item: AnyObject, offset: CGFloat = 0, priority: UILayoutPriority? = nil) -> NSLayoutConstraint { return self.constrainAttribute(.CenterX, toAttribute: .CenterX, ofItem: item, offset: offset, priority: priority) } /** Center the view vertically to an item. - Parameter item: Constraint the vertical axis of the view to this item, it may be a view or layout guide. - Parameter offset: How far to vertically offset the center of the view from the item's center. - Parameter priority: The priority of the constraints. - Returns: The constraint object. */ public func centerVerticallyToItem(item: AnyObject, offset: CGFloat = 0, priority: UILayoutPriority? = nil) -> NSLayoutConstraint { return self.constrainAttribute(.CenterY, toAttribute: .CenterY, ofItem: item, offset: offset, priority: priority) } // MARK: - Size /** Constrain the width of the view. - Parameter width: The width to constrain the view to. - Parameter priority: The priority of the constraints. - Returns: The constraint object. */ - Parameter priority: The priority of the constraint. } /** Constrain the height of the view. - Parameter height: The width to constrain the view to. - Parameter priority: The priority of the constraints. - Returns: The constraint object. */ public func sizeHeight(height: CGFloat, priority: UILayoutPriority? = nil) -> NSLayoutConstraint{ return self.constrainSizeAttribute(.Height, size: height, priority: priority) } /** Constrain the width of the view to the width of an item. - Parameter item: Constrain the width of the view to the width of the item, it may be a view or layout guide. - Parameter offset: How far to offset the width of the view from the width of the item. - Parameter priority: The priority of the constraints. - Returns: The constraint object. */ public func sizeWidthToWidthOfItem(item: AnyObject, offset: CGFloat = 0, priority: UILayoutPriority? = nil) -> NSLayoutConstraint { return self.constrainAttribute(.Width, toAttribute: .Width, ofItem: item, offset: -(offset*2), priority: priority) } /** Constrain the height of the view to the height of an item. - Parameter item: Constrain the height of the view to the height of the item, it may be a view or layout guide. - Parameter offset: How far to offset the height of the view from the height of the item. - Parameter priority: The priority of the constraints. - Returns: The constraint object. */ public func sizeHeightToHeightOfItem(item: AnyObject, offset: CGFloat = 0, priority: UILayoutPriority? = nil) -> NSLayoutConstraint { return self.constrainAttribute(.Height, toAttribute: .Height, ofItem: item, offset: -(offset*2), priority: priority) } public func sizeHeightToWidthOfItem(item: AnyObject, priority: UILayoutPriority? = nil) -> NSLayoutConstraint { return self.constrainAttribute(.Height, toAttribute: .Width, ofItem:item, priority: priority) } public func sizeWidthToHeightOfItem(item: AnyObject, priority: UILayoutPriority? = nil) -> NSLayoutConstraint { return self.constrainAttribute(.Width, toAttribute: .Height, ofItem:item, priority: priority) } public func sizeHeightToWidthAspectRatio(aspectRatio: CGFloat, priority: UILayoutPriority? = nil) -> NSLayoutConstraint { return self.sizeHeightToWidthAspectRatioOfItem(aspectRatio, item: self, priority: priority) } public func sizeWidthToHeightAspectRatio(aspectRatio: CGFloat, priority: UILayoutPriority? = nil) -> NSLayoutConstraint { return self.sizeWidthToHeightAspectRatioOfItem(aspectRatio, item: self, priority: priority) } public func sizeHeightToWidthAspectRatioOfItem(aspectRatio: CGFloat, item: AnyObject, priority: UILayoutPriority? = nil) -> NSLayoutConstraint { return self.constrainAttribute(.Height, toAttribute: .Width, ofItem: item, multiplier: aspectRatio, priority: priority) } public func sizeWidthToHeightAspectRatioOfItem(aspectRatio: CGFloat, item: AnyObject, priority: UILayoutPriority? = nil) -> NSLayoutConstraint { return self.constrainAttribute(.Width, toAttribute: .Height, ofItem: item, multiplier: aspectRatio, priority: priority) } public func sizeWidthAndHeight(toSize: CGFloat, priority: UILayoutPriority? = nil) -> (width: NSLayoutConstraint, height: NSLayoutConstraint) { return ( self.sizeWidth(toSize, priority: priority), self.sizeHeight(toSize, priority: priority) ) } public func sizeToWidthAndHeightOfItem(item: AnyObject, offset: CGFloat = 0, priority: UILayoutPriority? = nil) -> (width: NSLayoutConstraint, height: NSLayoutConstraint) { return ( self.sizeWidthToWidthOfItem(item, offset: offset, priority: priority), - Returns: The constraint object or `nil` if the constraint could not be made because the views do not share a common super view. ) } // MARK: - Position public func positionAboveItem(item: AnyObject, offset: CGFloat = 0, priority: UILayoutPriority? = nil) -> NSLayoutConstraint { return self.constrainAttribute(.Bottom, toAttribute:.Top, ofItem:item, offset:-offset, priority: priority) } public func positionToTheRightOfItem(item: AnyObject, offset: CGFloat = 0, priority: UILayoutPriority? = nil) -> NSLayoutConstraint { return self.constrainAttribute(.Left, toAttribute:.Right, ofItem:item, offset:offset, priority: priority) } public func positionBelowItem(item: AnyObject, offset: CGFloat = 0, priority: UILayoutPriority? = nil) -> NSLayoutConstraint { return self.constrainAttribute(.Top, toAttribute:.Bottom, ofItem:item, offset:offset, priority: priority) } public func positionToTheLeftOfItem(item: AnyObject, offset: CGFloat = 0, priority: UILayoutPriority? = nil) -> NSLayoutConstraint { return self.constrainAttribute(.Right, toAttribute:.Left, ofItem:item, offset:-offset, priority: priority) } public func positionViewsAbove(views: [UIView], offset: CGFloat = 0, priority: UILayoutPriority? = nil) { assert(views.count > 0, "Can only distribute 1 or more views") } /** Position the view below an item. } } public func positionViewsToTheRight(views: [UIView], offset: CGFloat = 0, priority: UILayoutPriority? = nil) { assert(views.count > 0, "Can only distribute 1 or more views") /** Position the view to the left of an item. - Parameter item: Position the view to the left of this item, it may be a view or layout guide. } } public func positionViewsBelow(views: [UIView], offset: CGFloat = 0, priority: UILayoutPriority? = nil) { assert(views.count > 0, "Can only distribute 1 or more views") extension UIView { /** Fit the view between the top and bottom items. } } public func positionViewsToTheLeft(views: [UIView], offset: CGFloat = 0, priority: UILayoutPriority? = nil) { assert(views.count > 0, "Can only distribute 1 or more views") ) } /** Fit the view between the left and right items. // MARK: - Between public func fitBetween(topItem: AnyObject, bottomItem: AnyObject, offset: CGFloat = 0, priority: UILayoutPriority? = nil) -> (top: NSLayoutConstraint, bottom: NSLayoutConstraint){ return ( self.positionBelowItem(topItem, offset: offset, priority: priority), ) } public func fitBetween(leftItem: AnyObject, rightItem: AnyObject, offset: CGFloat = 0, priority: UILayoutPriority? = nil) -> (left: NSLayoutConstraint, right: NSLayoutConstraint){ return ( self.positionToTheRightOfItem(leftItem, offset: offset, priority: priority), } var lastView: UIView! // MARK: - Fill public func fillHorizontally(views: [UIView], separation: CGFloat = 0, priority: UILayoutPriority? = nil) { assert(views.count > 1, "Can only distribute 2 or more views") /** Vertically layout and fill the view with views. - Parameter withViews: An array of views to vertically layout and fill the view with. - Parameter separation: The separation between the views. - Parameter priority: The priority of the constraints. */ public func fillVertically(withViews views: [UIView], separation: CGFloat = 0, priority: UILayoutPriority? = nil) { assert(views.count > 0, "Can only distribute 1 or more views") if views.count == 1 { lastView?.pinToRightEdgeOfSuperview(separation, priority: priority) } public func fillVertically(views: [UIView], separation: CGFloat = 0, priority: UILayoutPriority? = nil) { assert(views.count > 1, "Can only distribute 2 or more views") } _ = lastView?.pinToBottomEdgeOfSuperview(withOffset: separation, priority: priority) } } // MARK: - Bound extension UIView { /** Horizontally layout views and then bound the left and right edges of the view to the left and right edges of the first and last views. This method is useful for filling scroll views with views becuase the passed views will be laid out, // MARK: - Bound public func boundHorizontally(views: [UIView], separation: CGFloat = 0, priority: UILayoutPriority? = nil) { assert(views.count > 1, "Can only distribute 2 or more views") lastView = view } } self.pinLeftEdgeToLeftEdge(of: views.first!, withOffset: -separation, priority: priority) self.pinRightEdgeToRightEdge(of: views.last!, withOffset: -separation, priority: priority) } /** self.pinRightEdgeToRightEdgeOfItem(views.last!, offset: separation, priority: priority) } public func boundVertically(views: [UIView], separation: CGFloat = 0, priority: UILayoutPriority? = nil) { assert(views.count > 1, "Can only distribute 2 or more views") view.positionBelow(lastView, withOffset: separation, priority: priority) } lastView = view } } self.pinTopEdgeToTopEdge(of: views.first!, withOffset: -separation, priority: priority) self.pinBottomEdgeToBottomEdge(of: views.last!, withOffset: -separation, priority: priority) } } // MARK: - General extension UIView { /** Wrapper for the standard `NSLayoutConstraint` with some default values added for ease of use. This method adds the constraint to the view. This is the core method that every method in `KGNAutoLayout` uses deep down. - Parameter item: The item to constrain. - Parameter attribute: The attribute of the `item` to constrain. - Parameter toItem: The item to constrain the `item` to, defaults to nil. - Parameter attribute: The attribute of the `toItem` to constrain the `item` `attribute` to, defaults to `NotAnAttribute`. - Parameter relatedBy: The relation of the two items, deffaults to `Equal`. - Parameter multiplier: The multiplier of the constraint, defaults to `1`. - Parameter offset: The offset(`constant`) of the constraint, defaults to `0`. - Parameter priority: The priority of the constraint, defaults to `nil`. - Returns: The constraint object. */ @discardableResult public func constrain(item: LayoutItem, attribute itemAttribute: NSLayoutConstraint.Attribute, toItem: LayoutItem? = nil, attribute toAttribute: NSLayoutConstraint.Attribute = .notAnAttribute, relatedBy: NSLayoutConstraint.Relation = .equal, multiplier: CGFloat = 1, offset: CGFloat = 0, priority: UILayoutPriority? = nil) -> NSLayoutConstraint { if let view = item as? UIView { view.translatesAutoresizingMaskIntoConstraints = false } let constraint = NSLayoutConstraint( item: item, attribute: itemAttribute, relatedBy: relatedBy, toItem: toItem, attribute: toAttribute, multiplier: multiplier, constant: offset ) if priority != nil { constraint.priority = priority! } self.addConstraint(constraint) return constraint } } } startView = startView.superview } while (startView != nil && commonSuperview == nil) return commonSuperview }() }() assert(commonSuperview != nil, "Can't create constraints without a common superview") self.translatesAutoresizingMaskIntoConstraints = false public func centerHorizontally(to item: LayoutItem, withSeparation separation: CGFloat = 0, priority: UILayoutPriority? = nil) { assert(self.count > 0, "Can only center 1 or more views") if self.count % 2 == 0 { // even let rightIndex = self.count/2 let leftIndex = rightIndex-1 _ = self[leftIndex].constrain(attribute: .right, toAttribute: .centerX, ofItem: item, relatedBy: .lessThanOrEqual, offset:-separation/2, priority: priority) _ = self[rightIndex].constrain(attribute: .left, toAttribute: .centerX, ofItem: item, relatedBy: .lessThanOrEqual, offset:separation/2, priority: priority) if self.count > 2 { let rightViews = Array(self[rightIndex+1...self.count-1]) if rightViews.count > 0 { rightViews.positionToTheRight(of: self[rightIndex], withOffset: separation, priority: priority) } let leftViews = Array(self[0...leftIndex-1]) if leftViews.count > 0 { leftViews.positionToTheLeft(of: self[leftIndex], withOffset: separation, priority: priority) } } } else { // odd let centerIndex = self.count/2 self[centerIndex].centerHorizontally(to: item) if self.count > 1 { let rightViews = Array(self[centerIndex+1...self.count-1]) if rightViews.count > 0 { rightViews.positionToTheRight(of: self[centerIndex], withOffset: separation, priority: priority) } let leftViews = Array(self[0...centerIndex-1]) if leftViews.count > 0 { leftViews.positionToTheLeft(of: self[centerIndex], withOffset: separation, priority: priority) } } } } /** Center the views vertically within a view. - Parameter views: The views to center vertically. - Parameter separation: The separation between the views. - Parameter priority: The priority of the constraints. */ public func centerVertically(to item: LayoutItem, withSeparation separation: CGFloat = 0, priority: UILayoutPriority? = nil) { assert(self.count > 0, "Can only center 1 or more views") if self.count % 2 == 0 { // even let belowIndex = self.count/2 let aboveIndex = belowIndex-1 _ = self[aboveIndex].constrain(attribute: .bottom, toAttribute: .centerY, ofItem: item, relatedBy: .lessThanOrEqual, offset:-separation/2, priority: priority) _ = self[belowIndex].constrain(attribute: .top, toAttribute: .centerY, ofItem: item, relatedBy: .lessThanOrEqual, offset:separation/2, priority: priority) if self.count > 2 { let belowViews = Array(self[belowIndex+1...self.count-1]) if belowViews.count > 0 { belowViews.positionBelow(self[belowIndex], withOffset: separation, priority: priority) } let aboveViews = Array(self[0...aboveIndex-1]) if aboveViews.count > 0 { aboveViews.positionAbove(self[aboveIndex], withOffset: separation, priority: priority) } } }else{ // ood let centerIndex = self.count/2 self[centerIndex].centerVertically(to: item) if self.count > 1 { let belowViews = Array(self[centerIndex+1...self.count-1]) if belowViews.count > 0 { belowViews.positionBelow(self[centerIndex], withOffset: separation, priority: priority) } let aboveViews = Array(self[0...centerIndex-1]) if aboveViews.count > 0 { aboveViews.positionAbove(self[centerIndex], withOffset: separation, priority: priority) } } } } /** Position the views above the item. - Parameter item: The item to position the views above. - Parameter withOffset: How far to offset(seperate) the views. - Parameter priority: The priority of the constraints. */ public func positionAbove(_ item: LayoutItem, withOffset offset: CGFloat = 0, priority: UILayoutPriority? = nil) { var lastItem = item for view in self.reversed() { view.positionAbove(lastItem, withOffset: offset, priority: priority) lastItem = view } } /** Position the views to the right of the item. - Parameter item: The item to position the views to the right of. - Parameter withOffset: How far to offset(seperate) the views. - Parameter priority: The priority of the constraints. */ public func positionToTheRight(of item: LayoutItem, withOffset offset: CGFloat = 0, priority: UILayoutPriority? = nil) { var lastItem = item for view in self { view.positionToTheRight(of: lastItem, withOffset: offset, priority: priority) lastItem = view } } /** Position the views below the item. - Parameter item: The item to position the views below. - Parameter withOffset: How far to offset(seperate) the views. - Parameter priority: The priority of the constraints. */ public func positionBelow(_ item: LayoutItem, withOffset offset: CGFloat = 0, priority: UILayoutPriority? = nil) { var lastItem = item for view in self { view.positionBelow(lastItem, withOffset: offset, priority: priority) lastItem = view } } /** Position the views to the left of the item. - Parameter item: The item to position the views to the left of. - Parameter withOffset: How far to offset(seperate) the views. - Parameter priority: The priority of the constraints. */ public func positionToTheLeft(of item: LayoutItem, withOffset offset: CGFloat = 0, priority: UILayoutPriority? = nil) { var lastItem = item for view in self.reversed() { view.positionToTheLeft(of: lastItem, withOffset: offset, priority: priority) lastItem = view } } } // MARK: - Private extension UIView { fileprivate func constrain(sizeAttribute: NSLayoutConstraint.Attribute, size: CGFloat = 0, relatedBy: NSLayoutConstraint.Relation = .equal, priority: UILayoutPriority? = nil) -> NSLayoutConstraint { return self.constrain(item: self, attribute: sizeAttribute, relatedBy: relatedBy, multiplier: 0, offset: size, priority: priority) } fileprivate func constrain(edgeAttribute: NSLayoutConstraint.Attribute, offset: CGFloat = 0, priority: UILayoutPriority? = nil) -> NSLayoutConstraint? { assert(self.superview != nil, "Can't create constraints without a super view") guard let superview = self.superview else { return nil } return superview.constrain(item: self, attribute: edgeAttribute, toItem: superview, attribute: edgeAttribute, offset: offset, priority: priority) } fileprivate func constrain(attribute: NSLayoutConstraint.Attribute, toAttribute itemAttribute: NSLayoutConstraint.Attribute, ofItem item: LayoutItem, relatedBy: NSLayoutConstraint.Relation = .equal, multiplier: CGFloat = 1, offset: CGFloat = 0, priority: UILayoutPriority? = nil) -> NSLayoutConstraint? { let commonSuperview: UIView? = { guard let view = item as? UIView else { return self.superview } return { var startView: UIView! = self var commonSuperview: UIView? repeat { if view.isDescendant(of: startView) { commonSuperview = startView } startView = startView.superview } while (startView != nil && commonSuperview == nil) return commonSuperview }() }() assert(commonSuperview != nil, "Can't create constraints without a common super view") if commonSuperview == nil { return nil } return commonSuperview!.constrain(item: self, attribute: attribute, toItem: item, attribute: itemAttribute, relatedBy: relatedBy, multiplier: multiplier, offset: offset, priority: priority) } } <MSG> fully document code and remove old obj-c code <DFF> @@ -10,14 +10,14 @@ import UIKit extension UIView { -// MARK: - Pin: Superview + // MARK: - Pin: Superview /** Pin a view to all four edges of it's super view, with an offset(inset from the edges). - Parameter offset: How far to offset(inset) the edges of the view from the superview's edges. - Parameter priority: The priority of the constraints. - + - Returns: The top, right, bottom, and left constraint objects. */ public func pinToEdgesOfSuperview(offset: CGFloat = 0, priority: UILayoutPriority? = nil) -> (top: NSLayoutConstraint, right: NSLayoutConstraint, bottom: NSLayoutConstraint, left: NSLayoutConstraint) { @@ -30,62 +30,62 @@ extension UIView { } /** - Pin the top edge of the view to the top edge of it's superview. + Pin the top edge of the view to the top edge of it's superview. - - Parameter offset: How far to offset the top edge of the view from the top edge of its superview. - - Parameter priority: The priority of the constraint. + - Parameter offset: How far to offset the top edge of the view from the top edge of its superview. + - Parameter priority: The priority of the constraint. - - Returns: The constraint object. - */ + - Returns: The constraint object. + */ public func pinToTopEdgeOfSuperview(offset: CGFloat = 0, priority: UILayoutPriority? = nil) -> NSLayoutConstraint { return self.constrainEdgeAttribute(.Top, offset: offset, priority: priority) } /** - Pin the right edge of the view to the right edge of it's superview. + Pin the right edge of the view to the right edge of it's superview. - - Parameter offset: How far to offset the right edge of the view from the right edge of its superview. - - Parameter priority: The priority of the constraint. + - Parameter offset: How far to offset the right edge of the view from the right edge of its superview. + - Parameter priority: The priority of the constraint. - - Returns: The constraint object. - */ + - Returns: The constraint object. + */ public func pinToRightEdgeOfSuperview(offset: CGFloat = 0, priority: UILayoutPriority? = nil) -> NSLayoutConstraint { return self.constrainEdgeAttribute(.Right, offset: -offset, priority: priority) } /** - Pin the bottom edge of the view to the bottom edge of it's superview. + Pin the bottom edge of the view to the bottom edge of it's superview. - - Parameter offset: How far to offset the bottom edge of the view from the bottom edge of its superview. - - Parameter priority: The priority of the constraint. + - Parameter offset: How far to offset the bottom edge of the view from the bottom edge of its superview. + - Parameter priority: The priority of the constraint. - - Returns: The constraint object. - */ + - Returns: The constraint object. + */ public func pinToBottomEdgeOfSuperview(offset: CGFloat = 0, priority: UILayoutPriority? = nil) -> NSLayoutConstraint { return self.constrainEdgeAttribute(.Bottom, offset: -offset, priority: priority) } /** - Pin the left edge of the view to the left edge of it's superview. + Pin the left edge of the view to the left edge of it's superview. - - Parameter offset: How far to offset the left edge of the view from the bottom edge of its superview. - - Parameter priority: The priority of the constraint. + - Parameter offset: How far to offset the left edge of the view from the bottom edge of its superview. + - Parameter priority: The priority of the constraint. - - Returns: The constraint object. - */ + - Returns: The constraint object. + */ public func pinToLeftEdgeOfSuperview(offset: CGFloat = 0, priority: UILayoutPriority? = nil) -> NSLayoutConstraint { return self.constrainEdgeAttribute(.Left, offset: offset, priority: priority) } /** - Pin the left and right edges of the view to the left and right edges of it's superview. + Pin the left and right edges of the view to the left and right edges of it's superview. - - Parameter offset: How far to offset the left and right edges of the view from the left and right edges of its superview. - - Parameter priority: The priority of the constraints. + - Parameter offset: How far to offset the left and right edges of the view from the left and right edges of its superview. + - Parameter priority: The priority of the constraints. - - Returns: The left and right constraint objects. - */ + - Returns: The left and right constraint objects. + */ public func pinToSideEdgesOfSuperview(offset: CGFloat = 0, priority: UILayoutPriority? = nil) -> (left: NSLayoutConstraint, right: NSLayoutConstraint) { return ( self.pinToLeftEdgeOfSuperview(offset, priority: priority), @@ -94,13 +94,13 @@ extension UIView { } /** - Pin the top and bottom edges of the view to the top and bottom edges of it's superview. + Pin the top and bottom edges of the view to the top and bottom edges of it's superview. - - Parameter offset: How far to offset the top and bottom edges of the view from the top and bottom edges of its superview. - - Parameter priority: The priority of the constraints. + - Parameter offset: How far to offset the top and bottom edges of the view from the top and bottom edges of its superview. + - Parameter priority: The priority of the constraints. - - Returns: The top and bottom constraint objects. - */ + - Returns: The top and bottom constraint objects. + */ public func pinToTopAndBottomEdgesOfSuperview(offset: CGFloat = 0, priority: UILayoutPriority? = nil) -> (top: NSLayoutConstraint, bottom: NSLayoutConstraint) { return ( self.pinToTopEdgeOfSuperview(offset, priority: priority), @@ -108,14 +108,14 @@ extension UIView { ) } -// MARK: - Pin: Edges + // MARK: - Pin: Edges /** Pin the top edge of the view to the top edge of another item. - Parameter item: Constrain the top edge of the view to the top edge of this item, it may be a view or layout guide. - Parameter offset: How far to offset the top edge of the view from the top edge of the item. - - Parameter priority: The priority of the constraints. + - Parameter priority: The priority of the constraint. - Returns: The constraint object. */ @@ -124,45 +124,45 @@ extension UIView { } /** - Pin the right edge of the view to the right edge of another item. + Pin the right edge of the view to the right edge of another item. - - Parameter item: Constrain the right edge of the view to the right edge of this item, it may be a view or layout guide. - - Parameter offset: How far to offset the right edge of the view from the right edge of the item. - - Parameter priority: The priority of the constraints. + - Parameter item: Constrain the right edge of the view to the right edge of this item, it may be a view or layout guide. + - Parameter offset: How far to offset the right edge of the view from the right edge of the item. + - Parameter priority: The priority of the constraint. - - Returns: The constraint object. - */ + - Returns: The constraint object. + */ public func pinRightEdgeToRightEdgeOfItem(item: AnyObject, offset: CGFloat = 0, priority: UILayoutPriority? = nil) -> NSLayoutConstraint { return self.constrainAttribute(.Right, toAttribute: .Right, ofItem: item, offset: offset, priority: priority) } /** - Pin the bottom edge of the view to the bottom edge of another item. + Pin the bottom edge of the view to the bottom edge of another item. - - Parameter item: Constrain the bottom edge of the view to the bottom edge of this item, it may be a view or layout guide. - - Parameter offset: How far to offset the bottom edge of the view from the bottom edge of the item. - - Parameter priority: The priority of the constraints. + - Parameter item: Constrain the bottom edge of the view to the bottom edge of this item, it may be a view or layout guide. + - Parameter offset: How far to offset the bottom edge of the view from the bottom edge of the item. + - Parameter priority: The priority of the constraint. - - Returns: The constraint object. - */ + - Returns: The constraint object. + */ public func pinBottomEdgeToBottomEdgeOfItem(item: AnyObject, offset: CGFloat = 0, priority: UILayoutPriority? = nil) -> NSLayoutConstraint { return self.constrainAttribute(.Bottom, toAttribute: .Bottom, ofItem: item, offset: offset, priority: priority) } /** - Pin the left edge of the view to the left edge of another item. - - - Parameter item: Constrain the left edge of the view to the left edge of this item, it may be a view or layout guide. - - Parameter offset: How far to offset the left edge of the view from the left edge of the item. - - Parameter priority: The priority of the constraints. - - - Returns: The constraint object. - */ + Pin the left edge of the view to the left edge of another item. + + - Parameter item: Constrain the left edge of the view to the left edge of this item, it may be a view or layout guide. + - Parameter offset: How far to offset the left edge of the view from the left edge of the item. + - Parameter priority: The priority of the constraint. +  + - Returns: The constraint object. + */ public func pinLeftEdgeToLeftEdgeOfItem(item: AnyObject, offset: CGFloat = 0, priority: UILayoutPriority? = nil) -> NSLayoutConstraint { return self.constrainAttribute(.Left, toAttribute: .Left, ofItem: item, offset: -offset, priority: priority) } -// MARK: - Center + // MARK: - Center /** Center the view horizontally and vertically within it's superview. @@ -180,36 +180,36 @@ extension UIView { } /** - Center the view horizontally within it's super view. + Center the view horizontally within it's super view. - - Parameter offset: How far to horizontally offset the center of the view from the superview's center. - - Parameter priority: The priority of the constraints. -  - - Returns: The constraint object. - */ + - Parameter offset: How far to horizontally offset the center of the view from the superview's center. + - Parameter priority: The priority of the constraint. +  + - Returns: The constraint object. + */ public func centerHorizontallyInSuperview(offset: CGFloat = 0, priority: UILayoutPriority? = nil) -> NSLayoutConstraint { return self.constrainEdgeAttribute(.CenterX, offset: offset, priority: priority) } /** - Center the view vertical within it's super view. + Center the view vertical within it's super view. - - Parameter offset: How far to vertical offset the center of the view from the superview's center. - - Parameter priority: The priority of the constraints. -  - - Returns: The constraint object. - */ + - Parameter offset: How far to vertical offset the center of the view from the superview's center. + - Parameter priority: The priority of the constraint. +  + - Returns: The constraint object. + */ public func centerVerticallyInSuperview(offset: CGFloat = 0, priority: UILayoutPriority? = nil) -> NSLayoutConstraint { return self.constrainEdgeAttribute(.CenterY, offset: offset, priority: priority) } /** - Center the views horizontally within a view. + Center the views horizontally within a view. - - Parameter views: The views to center horizontally. - - Parameter separation: The separation between the views. - - Parameter priority: The priority of the constraints. - */ + - Parameter views: The views to center horizontally. + - Parameter separation: The separation between the views. + - Parameter priority: The priority of the constraints. + */ public func centerViewsHorizontally(views: [UIView], separation: CGFloat = 0, priority: UILayoutPriority? = nil) { assert(views.count > 0, "Can only distribute 1 or more views") @@ -247,12 +247,12 @@ extension UIView { } /** - Center the views vertically within a view. + Center the views vertically within a view. - - Parameter views: The views to center vertically. - - Parameter separation: The separation between the views. - - Parameter priority: The priority of the constraints. - */ + - Parameter views: The views to center vertically. + - Parameter separation: The separation between the views. + - Parameter priority: The priority of the constraints. + */ public func centerViewsVertically(views: [UIView], separation: CGFloat = 0, priority: UILayoutPriority? = nil) { assert(views.count > 0, "Can only distribute 1 or more views") @@ -294,39 +294,39 @@ extension UIView { } /** - Center the view horizontally to an item. + Center the view horizontally to an item. - - Parameter item: Constraint the horizontal axis of the view to this item, it may be a view or layout guide. - - Parameter offset: How far to horizontally offset the center of the view from the item's center. - - Parameter priority: The priority of the constraints. - - - Returns: The constraint object. - */ + - Parameter item: Constraint the horizontal axis of the view to this item, it may be a view or layout guide. + - Parameter offset: How far to horizontally offset the center of the view from the item's center. + - Parameter priority: The priority of the constraint. + + - Returns: The constraint object. + */ public func centerHorizontallyToItem(item: AnyObject, offset: CGFloat = 0, priority: UILayoutPriority? = nil) -> NSLayoutConstraint { return self.constrainAttribute(.CenterX, toAttribute: .CenterX, ofItem: item, offset: offset, priority: priority) } /** - Center the view vertically to an item. + Center the view vertically to an item. - - Parameter item: Constraint the vertical axis of the view to this item, it may be a view or layout guide. - - Parameter offset: How far to vertically offset the center of the view from the item's center. - - Parameter priority: The priority of the constraints. + - Parameter item: Constraint the vertical axis of the view to this item, it may be a view or layout guide. + - Parameter offset: How far to vertically offset the center of the view from the item's center. + - Parameter priority: The priority of the constraint. - - Returns: The constraint object. - */ + - Returns: The constraint object. + */ public func centerVerticallyToItem(item: AnyObject, offset: CGFloat = 0, priority: UILayoutPriority? = nil) -> NSLayoutConstraint { return self.constrainAttribute(.CenterY, toAttribute: .CenterY, ofItem: item, offset: offset, priority: priority) } -// MARK: - Size + // MARK: - Size /** Constrain the width of the view. - Parameter width: The width to constrain the view to. - - Parameter priority: The priority of the constraints. + - Parameter priority: The priority of the constraint. - Returns: The constraint object. */ @@ -335,74 +335,90 @@ extension UIView { } /** - Constrain the height of the view. + Constrain the height of the view. - - Parameter height: The width to constrain the view to. - - Parameter priority: The priority of the constraints. + - Parameter height: The width to constrain the view to. + - Parameter priority: The priority of the constraint. - - Returns: The constraint object. - */ + - Returns: The constraint object. + */ public func sizeHeight(height: CGFloat, priority: UILayoutPriority? = nil) -> NSLayoutConstraint{ return self.constrainSizeAttribute(.Height, size: height, priority: priority) } /** - Constrain the width of the view to the width of an item. + Constrain the width and height of the view. - - Parameter item: Constrain the width of the view to the width of the item, it may be a view or layout guide. - - Parameter offset: How far to offset the width of the view from the width of the item. - - Parameter priority: The priority of the constraints. + - Parameter size: The width and height to constrain the view to. + - Parameter priority: The priority of the constraint. - - Returns: The constraint object. - */ + - Returns: The constraint object. + */ + public func sizeWidthAndHeight(size: CGFloat, priority: UILayoutPriority? = nil) -> (width: NSLayoutConstraint, height: NSLayoutConstraint) { + return ( + self.sizeWidth(size, priority: priority), + self.sizeHeight(size, priority: priority) + ) + } + + /** + Constrain the width of the view to the width of an item. + + - Parameter item: Constrain the width of the view to the width of the item, it may be a view or layout guide. + - Parameter offset: How far to offset the width of the view from the width of the item. + - Parameter priority: The priority of the constraint. + + - Returns: The constraint object. + */ public func sizeWidthToWidthOfItem(item: AnyObject, offset: CGFloat = 0, priority: UILayoutPriority? = nil) -> NSLayoutConstraint { return self.constrainAttribute(.Width, toAttribute: .Width, ofItem: item, offset: -(offset*2), priority: priority) } /** - Constrain the height of the view to the height of an item. + Constrain the height of the view to the height of an item. - - Parameter item: Constrain the height of the view to the height of the item, it may be a view or layout guide. - - Parameter offset: How far to offset the height of the view from the height of the item. - - Parameter priority: The priority of the constraints. + - Parameter item: Constrain the height of the view to the height of the item, it may be a view or layout guide. + - Parameter offset: How far to offset the height of the view from the height of the item. + - Parameter priority: The priority of the constraint. - - Returns: The constraint object. - */ + - Returns: The constraint object. + */ public func sizeHeightToHeightOfItem(item: AnyObject, offset: CGFloat = 0, priority: UILayoutPriority? = nil) -> NSLayoutConstraint { return self.constrainAttribute(.Height, toAttribute: .Height, ofItem: item, offset: -(offset*2), priority: priority) } + /** + Constrain the height of the view to the width of an item. + + - Parameter item: Constrain the height of the view to the width of this item, it may be a view or layout guide. + - Parameter priority: The priority of the constraint. + + - Returns: The constraint object. + */ public func sizeHeightToWidthOfItem(item: AnyObject, priority: UILayoutPriority? = nil) -> NSLayoutConstraint { return self.constrainAttribute(.Height, toAttribute: .Width, ofItem:item, priority: priority) } - public func sizeWidthToHeightOfItem(item: AnyObject, priority: UILayoutPriority? = nil) -> NSLayoutConstraint { - return self.constrainAttribute(.Width, toAttribute: .Height, ofItem:item, priority: priority) - } - - public func sizeHeightToWidthAspectRatio(aspectRatio: CGFloat, priority: UILayoutPriority? = nil) -> NSLayoutConstraint { - return self.sizeHeightToWidthAspectRatioOfItem(aspectRatio, item: self, priority: priority) - } + /** + Constrain the width of the view to the height of an item. - public func sizeWidthToHeightAspectRatio(aspectRatio: CGFloat, priority: UILayoutPriority? = nil) -> NSLayoutConstraint { - return self.sizeWidthToHeightAspectRatioOfItem(aspectRatio, item: self, priority: priority) - } + - Parameter item: Constrain the width of the view to the height of this item, it may be a view or layout guide. + - Parameter priority: The priority of the constraint. - public func sizeHeightToWidthAspectRatioOfItem(aspectRatio: CGFloat, item: AnyObject, priority: UILayoutPriority? = nil) -> NSLayoutConstraint { - return self.constrainAttribute(.Height, toAttribute: .Width, ofItem: item, multiplier: aspectRatio, priority: priority) + - Returns: The constraint object. + */ + public func sizeWidthToHeightOfItem(item: AnyObject, priority: UILayoutPriority? = nil) -> NSLayoutConstraint { + return self.constrainAttribute(.Width, toAttribute: .Height, ofItem:item, priority: priority) } - public func sizeWidthToHeightAspectRatioOfItem(aspectRatio: CGFloat, item: AnyObject, priority: UILayoutPriority? = nil) -> NSLayoutConstraint { - return self.constrainAttribute(.Width, toAttribute: .Height, ofItem: item, multiplier: aspectRatio, priority: priority) - } + /** + Constrain the width and height of the view to the width and height of an item. - public func sizeWidthAndHeight(toSize: CGFloat, priority: UILayoutPriority? = nil) -> (width: NSLayoutConstraint, height: NSLayoutConstraint) { - return ( - self.sizeWidth(toSize, priority: priority), - self.sizeHeight(toSize, priority: priority) - ) - } + - Parameter item: Constrain the width and height of the view to the width and height of the item, it may be a view or layout guide. + - Parameter priority: The priority of the constraints. + - Returns: The width and height constraint objects. + */ public func sizeToWidthAndHeightOfItem(item: AnyObject, offset: CGFloat = 0, priority: UILayoutPriority? = nil) -> (width: NSLayoutConstraint, height: NSLayoutConstraint) { return ( self.sizeWidthToWidthOfItem(item, offset: offset, priority: priority), @@ -410,24 +426,92 @@ extension UIView { ) } + /** + Constrain the height to a width aspect ratio. + + - Parameter aspectRatio: The aspect ratio of the width to constrain the height to. + - Parameter priority: The priority of the constraints. + + - Returns: The constraint object. + */ + public func sizeHeightToWidthAspectRatio(aspectRatio: CGFloat, priority: UILayoutPriority? = nil) -> NSLayoutConstraint { + return self.constrainAttribute(.Height, toAttribute: .Width, ofItem: self, multiplier: aspectRatio, priority: priority) + } + + /** + Constrain the width to a height aspect ratio. + + - Parameter aspectRatio: The aspect ratio of the height to constrain the width to. + - Parameter priority: The priority of the constraints. + + - Returns: The constraint object. + */ + public func sizeWidthToHeightAspectRatio(aspectRatio: CGFloat, priority: UILayoutPriority? = nil) -> NSLayoutConstraint { + return self.constrainAttribute(.Width, toAttribute: .Height, ofItem: self, multiplier: aspectRatio, priority: priority) + } + // MARK: - Position + /** + Position the view above an item. + + - Parameter item: Position the view above this item, it may be a view or layout guide. + - Parameter offset: How far to offset(seperate) the bottom of the view from the top of the item. + - Parameter priority: The priority of the constraint. + + - Returns: The constraint object. + */ public func positionAboveItem(item: AnyObject, offset: CGFloat = 0, priority: UILayoutPriority? = nil) -> NSLayoutConstraint { return self.constrainAttribute(.Bottom, toAttribute:.Top, ofItem:item, offset:-offset, priority: priority) } + /** + Position the view to the right of an item. + + - Parameter item: Position the view to the right of this item, it may be a view or layout guide. + - Parameter offset: How far to offset(seperate) the left of the view from the right of the item. + - Parameter priority: The priority of the constraint. + + - Returns: The constraint object. + */ public func positionToTheRightOfItem(item: AnyObject, offset: CGFloat = 0, priority: UILayoutPriority? = nil) -> NSLayoutConstraint { return self.constrainAttribute(.Left, toAttribute:.Right, ofItem:item, offset:offset, priority: priority) } + /** + Position the view below an item. + + - Parameter item: Position the view below this item, it may be a view or layout guide. + - Parameter offset: How far to offset(seperate) the top of the view from the bottom of the item. + - Parameter priority: The priority of the constraint. + + - Returns: The constraint object. + */ public func positionBelowItem(item: AnyObject, offset: CGFloat = 0, priority: UILayoutPriority? = nil) -> NSLayoutConstraint { return self.constrainAttribute(.Top, toAttribute:.Bottom, ofItem:item, offset:offset, priority: priority) } + + /** + Position the view to the left of an item. + + - Parameter item: Position the view to the left of this item, it may be a view or layout guide. + - Parameter offset: How far to offset(seperate) the right of the view from the left of the item. + - Parameter priority: The priority of the constraint. + + - Returns: The constraint object. + */ public func positionToTheLeftOfItem(item: AnyObject, offset: CGFloat = 0, priority: UILayoutPriority? = nil) -> NSLayoutConstraint { return self.constrainAttribute(.Right, toAttribute:.Left, ofItem:item, offset:-offset, priority: priority) } + /** + Position the views above the view. + + - Parameter views: The views to position above the view. + - Parameter offset: How far to offset(seperate) the views. + - Parameter priority: The priority of the constraints. + */ public func positionViewsAbove(views: [UIView], offset: CGFloat = 0, priority: UILayoutPriority? = nil) { assert(views.count > 0, "Can only distribute 1 or more views") @@ -438,6 +522,13 @@ extension UIView { } } + /** + Position the views to the right of the view. + + - Parameter views: The views to position to the right of the view. + - Parameter offset: How far to offset(seperate) the views. + - Parameter priority: The priority of the constraints. + */ public func positionViewsToTheRight(views: [UIView], offset: CGFloat = 0, priority: UILayoutPriority? = nil) { assert(views.count > 0, "Can only distribute 1 or more views") @@ -448,6 +539,13 @@ extension UIView { } } + /** + Position the views below the view. + + - Parameter views: The views to position below the view. + - Parameter offset: How far to offset(seperate) the views. + - Parameter priority: The priority of the constraints. + */ public func positionViewsBelow(views: [UIView], offset: CGFloat = 0, priority: UILayoutPriority? = nil) { assert(views.count > 0, "Can only distribute 1 or more views") @@ -458,6 +556,13 @@ extension UIView { } } + /** + Position the views to the left of the view. + + - Parameter views: The views to position to the left of the view. + - Parameter offset: How far to offset(seperate) the views. + - Parameter priority: The priority of the constraints. + */ public func positionViewsToTheLeft(views: [UIView], offset: CGFloat = 0, priority: UILayoutPriority? = nil) { assert(views.count > 0, "Can
{'addition_count': 290, 'commit_subject': 'fully document code and remove old obj-c code', 'deletion_count': 133, 'file_extension': '.swift', 'lang': 'swift', 'license': 'mit', 'repo_name': 'kgn/KGNAutoLayout'}