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

    subview

    view to add as a child.

    constraints

    dictionary 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

    subview

    view to add as a child.

    insets

    Spacing 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
    ) -> NSLayoutConstraint

    Parameters

    attribute

    From NSLayoutConstraint.Attribute

    toAttribute

    To NSLayoutConstraint.Attribute

    view2

    To view

    relation

    NSLayoutConstraint.Relation between the views

    multiplier

    CGFloat to multiply the constant

    constant

    Distance for the constraint

    priority

    UILayoutPriority for the constraint

    isActive

    Bool 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

    size

    CGSize describing the width and the height to apply

    relation

    NSLayoutConstraint.Relation between the views

    priority

    UILayoutPriority 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
    ) -> NSLayoutConstraint

    Parameters

    width

    CGFloat describing the width to apply

    relation

    NSLayoutConstraint.Relation between the views

    priority

    UILayoutPriority 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
    ) -> NSLayoutConstraint

    Parameters

    width

    CGFloat describing the height to apply

    relation

    NSLayoutConstraint.Relation between the views

    priority

    UILayoutPriority 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

    view

    the 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) -> NSLayoutConstraint

    Parameters

    view

    the 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) -> NSLayoutConstraint

    Parameters

    view

    the view to which self will be centered to. nil implies to use self.superview instead.

    Return Value

    The applied constraint