UIView

extension UIView
extension UIView: Anchorable
  • Declarative auto-layout. Creates standard NSLayoutConstraint’s but in a declarative way and with fewer lines of code.

    Declaration

    Swift

    @discardableResult
    public func constrain(
        _ attr1: NSLayoutConstraint.Attribute,
        to attr2: NSLayoutConstraint.Attribute = .notAnAttribute,
        of view2: Any? = nil,
        relatedBy relation: NSLayoutConstraint.Relation = .equal,
        multiplier: CGFloat = 1,
        constant c: CGFloat = 0,
        priority: UILayoutPriority = .required,
        isActive: Bool = true
    ) -> NSLayoutConstraint

    Parameters

    attr1

    attribute to constrain

    attr2

    second attribute (optional, default .notAnAttribute)

    view2

    view for second attribute (optional, default nil)

    relation

    relation to evaluate (towards multiplier*attr2 + c) (default .equal)

    multiplier

    multiplier to apply (default 1)

    c

    constant offset amount (default 0)

    priority

    constraint priority (default .required)

    isActive

    whether to activate the constraint or not (default true)

    Return Value

    The created layout constraint

  • Anchors that constrain views along the x-axis

    See more

    Declaration

    Swift

    public enum XAxisAnchor
  • Anchors that constrain views along the y-axis

    See more

    Declaration

    Swift

    public enum YAxisAnchor
  • Anchors that constrain views along either width or height

    See more

    Declaration

    Swift

    public enum DimensionAnchor
  • Creates standard NSLayoutConstraint’s using anchor syntax

    Declaration

    Swift

    @discardableResult
    public func constrain(
        _ anchorType: XAxisAnchor,
        to otherAnchor: NSLayoutXAxisAnchor,
        relatedBy relation: NSLayoutConstraint.Relation = .equal,
        constant c: CGFloat = 0,
        priority: UILayoutPriority = .required,
        isActive: Bool = true
    ) -> NSLayoutConstraint

    Parameters

    anchorType

    X-axis anchor to constrain

    otherAnchor

    other X-axis anchor

    relation

    relation to evaluate (towards otherAnchor + c) (default .equal)

    c

    constant offset amount (default 0)

    priority

    constraint priority (default .required)

    isActive

    whether to activate the constraint or not (default true)

    Return Value

    The created layout constraint

  • Creates standard NSLayoutConstraint’s using anchor syntax

    Declaration

    Swift

    @discardableResult
    public func constrain(
        _ anchorType: YAxisAnchor,
        to otherAnchor: NSLayoutYAxisAnchor,
        relatedBy relation: NSLayoutConstraint.Relation = .equal,
        constant c: CGFloat = 0,
        priority: UILayoutPriority = .required,
        isActive: Bool = true
    ) -> NSLayoutConstraint

    Parameters

    anchorType

    Y-axis anchor to constrain

    otherAnchor

    other Y-axis anchor

    relation

    relation to evaluate (towards otherAnchor + c) (default .equal)

    c

    constant offset amount (default 0)

    priority

    constraint priority (default .required)

    isActive

    whether to activate the constraint or not (default true)

    Return Value

    The created layout constraint

  • Creates standard NSLayoutConstraint’s using anchor syntax

    Declaration

    Swift

    @discardableResult
    public func constrain(
        _ anchorType: DimensionAnchor,
        relatedBy relation: NSLayoutConstraint.Relation = .equal,
        constant c: CGFloat,
        priority: UILayoutPriority = .required,
        isActive: Bool = true
    ) -> NSLayoutConstraint

    Parameters

    anchorType

    layout dimension to constrain

    relation

    relation to evaluate (towards otherAnchor + c) (default .equal)

    c

    constant offset amount (default 0)

    priority

    constraint priority (default .required)

    isActive

    whether to activate the constraint or not (default true)

    Return Value

    The created layout constraint

  • Creates standard NSLayoutConstraint’s using anchor syntax

    Declaration

    Swift

    @discardableResult
    public func constrain(
        _ anchorType: DimensionAnchor,
        to otherAnchor: NSLayoutDimension,
        relatedBy relation: NSLayoutConstraint.Relation = .equal,
        multiplier: CGFloat = 1,
        constant c: CGFloat = 0,
        priority: UILayoutPriority = .required,
        isActive: Bool = true
    ) -> NSLayoutConstraint

    Parameters

    anchorType

    layout dimension to constrain

    otherAnchor

    other layout dimension anchor

    relation

    relation to evaluate (towards otherAnchor + c) (default .equal)

    multiplier

    multiplier to apply (default 1)

    c

    constant offset amount (default 0)

    priority

    constraint priority (default .required)

    isActive

    whether to activate the constraint or not (default true)

    Return Value

    The created layout constraint

  • Constrain the aspect ratio for the receiving view.

    Declaration

    Swift

    @discardableResult
    public func constrainAspectRatio(
        _ ratio: CGFloat,
        offset: CGFloat = 0,
        relatedBy relation: NSLayoutConstraint.Relation = .equal,
        priority: UILayoutPriority = .required,
        isActive: Bool = true
    ) -> NSLayoutConstraint

    Parameters

    ratio

    aspect ratio

    offset

    offset to apply (default .zero)

    relation

    relation to evaluate (towards ratio) (default .equal)

    priority

    constraint priority (default .required)

    isActive

    whether to activate the constraint or not (default true)

    Return Value

    the created layout constraint

  • Constrain the receiving view below a sibling view

    Declaration

    Swift

    @discardableResult
    public func constrain(
        below sibling: Anchorable,
        relatedBy relation: NSLayoutConstraint.Relation = .equal,
        offset: CGFloat = 0,
        priority: UILayoutPriority = .required,
        isActive: Bool = true
    ) -> NSLayoutConstraint

    Parameters

    sibling

    sibling view to constrain to

    relation

    relation to evaluate (towards sibling) (default .equal)

    offset

    the gap between the bottom of sibling and the top of the receiver (default 0)

    priority

    constraint priority (default .required)

    isActive

    whether to activate the constraint or not (default true)

    Return Value

    The created layout constraint

  • Constrain the receiving view to the left of (trailing side of) a sibling view

    Declaration

    Swift

    @discardableResult
    public func constrain(
        after sibling: Anchorable,
        relatedBy relation: NSLayoutConstraint.Relation = .equal,
        offset: CGFloat = 0,
        priority: UILayoutPriority = .required,
        isActive: Bool = true
    ) -> NSLayoutConstraint

    Parameters

    sibling

    sibling view to constrain to

    relation

    relation to evaluate (towards sibling) (default .equal)

    offset

    the gap between the trailing edge of sibling and the leading edge of the receiver (default 0)

    priority

    constraint priority (default .required)

    isActive

    whether to activate the constraint or not (default true)

    Return Value

    The created layout constraint

  • Center alignment options

    See more

    Declaration

    Swift

    public struct Center : OptionSet
  • Constrain the center of the receiving view with the center of another view

    Declaration

    Swift

    @discardableResult
    public func constrainCenter(
        _ center: Center = .all,
        to view2: Anchorable? = nil,
        relatedBy relation: NSLayoutConstraint.Relation = .equal,
        offset: UIOffset = .zero,
        priority: UILayoutPriority = .required,
        isActive: Bool = true
    ) -> [NSLayoutConstraint.Attribute: NSLayoutConstraint]

    Parameters

    center

    which center attributes to constrain (default .all)

    view2

    view or layout guide to constrain to (pass nil to constrain to superview)

    relation

    relation to evaluate (towards view2) (default .equal)

    offset

    offset to apply relative to view2.center (default .zero)

    priority

    constraint priority (default .required)

    isActive

    whether to activate the constraints or not (default true)

    Return Value

    dictionary of constraints created, keyed by .centerX, .centerY

  • Inset the edges of one view to those of another view

    Declaration

    Swift

    @discardableResult
    public func constrainEdges(
        _ edges: NSDirectionalRectEdge = .all,
        to view2: Anchorable? = nil,
        relatedBy relation: NSLayoutConstraint.Relation = .equal,
        with insets: NSDirectionalEdgeInsets = .zero,
        priority: UILayoutPriority = .required,
        isActive: Bool = true
    ) -> [NSLayoutConstraint.Attribute: NSLayoutConstraint]

    Parameters

    edges

    which edges to constrain (default .all)

    view2

    view or layout guide to constrain to (pass nil to constrain to superview)

    relation

    relation to evaluate (towards view2) (default .equal)

    insets

    insets to apply to each edge (default .zero)

    priority

    constraint priority (default .required)

    isActive

    whether to activate the constraints or not (default true)

    Return Value

    dictionary of constraints created, keyed by .top, .leading, .bottom, .trailing

  • Outset the edges of one view relative to those of another

    Declaration

    Swift

    @discardableResult
    public func constrainEdges(
        _ edges: NSDirectionalRectEdge = .all,
        to view2: UIView? = nil,
        relatedBy relation: NSLayoutConstraint.Relation = .equal,
        outsets: NSDirectionalEdgeInsets,
        priority: UILayoutPriority = .required,
        isActive: Bool = true
    ) -> [NSLayoutConstraint.Attribute: NSLayoutConstraint]

    Parameters

    edges

    which edges to constrain (default .all)

    view2

    view to constraint to (pass nil to constrain to superview)

    relation

    relation to evaluate (towards view2) (default .equal)

    outsets

    outsets to apply to each edge (default .zero)

    priority

    constraint priority (default .required)

    isActive

    whether to activate the constraints or not (default true)

    Return Value

    dictionary of constraints created, keyed by .top, .leading, .bottom, .trailing

  • Inset the edges of one view to the layout margins of another view

    Declaration

    Swift

    @discardableResult
    public func constrainEdgesToMargins(
        _ edges: NSDirectionalRectEdge = .all,
        to view2: UIView? = nil,
        relatedBy relation: NSLayoutConstraint.Relation = .equal,
        with insets: NSDirectionalEdgeInsets = .zero,
        priority: UILayoutPriority = .required,
        isActive: Bool = true
    ) -> [NSLayoutConstraint.Attribute: NSLayoutConstraint]

    Parameters

    edges

    which edges to constrain (default .all)

    view2

    view whose layout margins will be used to constrain the receiver (pass nil to constrain to superview.layoutMarginsGuide)

    relation

    relation to evaluate (towards view2) (default .equal)

    insets

    insets to apply to each edge (default .zero)

    priority

    constraint priority (default .required)

    isActive

    whether to activate the constraints or not (default true)

    Return Value

    dictionary of constraints created, keyed by .top, .leading, .bottom, .trailing

  • Constrain the receiving view with given size

    Declaration

    Swift

    @discardableResult
    public func constrainSize(
        _ size: CGSize,
        relatedBy relation: NSLayoutConstraint.Relation = .equal,
        priority: UILayoutPriority = .required,
        isActive: Bool = true
    ) -> [NSLayoutConstraint.Attribute: NSLayoutConstraint]

    Parameters

    size

    size of view to constrain to

    relation

    relation to evaluate (towards size) (default .equal)

    priority

    constraint priority (default .required)

    isActive

    whether to activate the constraint or not (default true)

    Return Value

    dictionary of constraints created, keyed by .width, .height

  • Constrain the receiving view with width and height

    Declaration

    Swift

    @discardableResult
    public func constrainSize(
        width: CGFloat,
        height: CGFloat,
        relatedBy relation: NSLayoutConstraint.Relation = .equal,
        priority: UILayoutPriority = .required,
        isActive: Bool = true
    ) -> [NSLayoutConstraint.Attribute: NSLayoutConstraint]

    Parameters

    width

    width of view to constrain to

    height

    height of view to constrain to

    relation

    relation to evaluate (towards width and height) (default .equal)

    priority

    constraint priority (default .required)

    isActive

    whether to activate the constraint or not (default true)

    Return Value

    dictionary of constraints created, keyed by .width, .height

  • Constrain the receiving view with given dimension applied to both width and height.

    Declaration

    Swift

    @discardableResult
    public func constrainSize(
        _ dimension: CGFloat,
        relatedBy relation: NSLayoutConstraint.Relation = .equal,
        priority: UILayoutPriority = .required,
        isActive: Bool = true
    ) -> [NSLayoutConstraint.Attribute: NSLayoutConstraint]

    Parameters

    dimension

    dimension of view to constrain to

    relation

    relation to evaluate (towards dimension) (default .equal)

    priority

    constraint priority (default .required)

    isActive

    whether to activate the constraint or not (default true)

    Return Value

    dictionary of constraints created, keyed by .width, .height