UIView
extension UIView
-
Adds the subview as a child and apply the constraints described.
- Usage example: add(childView, constrainedBy: [.top: .none, .leading: .small, .trailing: .small, .bottom: .none])
Declaration
Swift
@discardableResult public func addSubview( _ subview: UIView, constrainedBy constraints: [NSLayoutConstraint.Attribute: Spacing] ) -> [NSLayoutConstraint]Parameters
subviewview to add as a child.
constraintsdictionary with attributes keys and Spacing constants
Return Value
The array of constraints applied.
-
Convenience method that adds the subview as a child and apply the same inset to the 4 edges.
- Usage example: add(childView, insets: .medium)
Declaration
Swift
@discardableResult public func addSubview( _ subview: UIView, insets: Spacing ) -> [NSLayoutConstraint]Parameters
subviewview to add as a child.
insetsSpacing case to apply to the 4 edges (top, leading, trailing, bottom)
Return Value
The array of constraints applied.
-
Convenience method to disable the auto constraints.
Declaration
Swift
func disableAutoConstraints() -
Helper method to create a constraint between self and another view.
Declaration
Swift
@discardableResult public func pin( _ attribute: NSLayoutConstraint.Attribute, to toAttribute: NSLayoutConstraint.Attribute = .notAnAttribute, of view2: UIView? = nil, relatedBy relation: NSLayoutConstraint.Relation = .equal, multiplier: CGFloat = 1, constant: CGFloat = 0, priority: UILayoutPriority = .required, isActive: Bool = true ) -> NSLayoutConstraintParameters
attributeFromNSLayoutConstraint.AttributetoAttributeToNSLayoutConstraint.Attributeview2ToviewrelationNSLayoutConstraint.Relation between the views
multiplierCGFloat to multiply the constant
constantDistance for the constraint
priorityUILayoutPriority for the constraint
isActiveBool that indicates if the constraint will be active after the call
Return Value
The applied constraint
-
Convenience method to apply width and height constraints at the same time
Declaration
Swift
@discardableResult func pinSize( _ size: CGSize, relatedBy relation: NSLayoutConstraint.Relation = .equal, priority: UILayoutPriority = .required ) -> (width: NSLayoutConstraint, height: NSLayoutConstraint)Parameters
sizeCGSize describing the width and the height to apply
relationNSLayoutConstraint.Relation between the views
priorityUILayoutPriority for the constraints
Return Value
The applied constraints
-
Convenience method to apply a width constraint
Declaration
Swift
@discardableResult func pin( width: CGFloat, relatedBy relation: NSLayoutConstraint.Relation = .equal, priority: UILayoutPriority = .required ) -> NSLayoutConstraintParameters
widthCGFloat describing the width to apply
relationNSLayoutConstraint.Relation between the views
priorityUILayoutPriority for the constraints
Return Value
The applied width constraint
-
Convenience method to apply a height constraint
Declaration
Swift
@discardableResult func pin( height: CGFloat, relatedBy relation: NSLayoutConstraint.Relation = .equal, priority: UILayoutPriority = .required ) -> NSLayoutConstraintParameters
widthCGFloat describing the height to apply
relationNSLayoutConstraint.Relation between the views
priorityUILayoutPriority for the constraints
Return Value
The applied height constraint
-
Convenience method to pin self to the center (x and y) of another view
Declaration
Swift
@discardableResult func pinToCenter(of view: UIView? = nil) -> (horizontal: NSLayoutConstraint, vertical: NSLayoutConstraint)Parameters
viewthe view to which self will be centered to. nil implies to use self.superview instead.
Return Value
The applied constraints
-
Convenience method to pin self to the horizontal center (x) of another view
Declaration
Swift
@discardableResult func centerHorizontally(of view: UIView? = nil) -> NSLayoutConstraintParameters
viewthe view to which self will be centered to. nil implies to use self.superview instead.
Return Value
The applied constraint
-
Convenience method to pin self to the vertical center (y) of another view
Declaration
Swift
@discardableResult func centerVertically(of view: UIView? = nil) -> NSLayoutConstraintParameters
viewthe view to which self will be centered to. nil implies to use self.superview instead.
Return Value
The applied constraint
UIView Extension Reference