UIView

public extension UIView

Auto Layout and general utility methods for UIView.

  • Remove all subviews of the calling UIView.

    Author

    Daniel Loewenherz

    Date

    February 17, 2016

    Declaration

    Swift

    func removeSubviews()
  • Center the calling UIView on the X-axis, with optional margins to the right and left.

    Author

    Daniel Loewenherz

    Date

    March 9, 2016

    Declaration

    Swift

    @available(iOS 9, *)
    func centerOnXAxis(width: CGFloat? = nil)
  • Center the calling UIView on the Y-axis, with optional margins to the top and bottom.

    Author

    Daniel Loewenherz

    Date

    March 9, 2016

    Declaration

    Swift

    @available(iOS 9, *)
    func centerOnYAxis(height: CGFloat? = nil)
  • Set the height of this view to a specified value using Auto Layout.

    Author

    Daniel Loewenherz

    Date

    February 17, 2016

    Declaration

    Swift

    @available(iOS 9, *)
    func setHeight(_ height: CGFloat)

    Parameters

    height

    A Float specifying the view’s height.

  • Set the width of this view to a specified value using Auto Layout.

    Author

    Daniel Loewenherz

    Date

    February 17, 2016

    Declaration

    Swift

    @available(iOS 9, *)
    func setWidth(_ width: CGFloat)

    Parameters

    width

    A Float specifying the view’s width.

  • Set the size of the view using Auto Layout.

    Author

    Daniel Loewenherz

    Date

    February 17, 2016

    Declaration

    Swift

    @available(iOS 9.0, *)
    func setContentSize(_ size: CGSize)

    Parameters

    size

    A CGSize specifying the view’s size.

  • Undocumented

    Declaration

    Swift

    func fillHorizontalMarginsOfSuperview(margin: CGFloat = 0)
  • Pin the view’s horizontal edges to the left and right of its superview.

    Author

    Daniel Loewenherz

    Date

    February 17, 2016

    Declaration

    Swift

    @available(iOS 9, *)
    func fillWidthOfSuperview()
  • Pin the view’s vertical edges to the top and bottom of its superview.

    Author

    Daniel Loewenherz

    Date

    February 17, 2016

    Declaration

    Swift

    @available(iOS 9, *)
    func fillHeightOfSuperview()
  • Pin the view’s horizontal edges to the left and right of its superview with a margin.

    Author

    Daniel Loewenherz

    Date

    February 17, 2016

    Declaration

    Swift

    @available(iOS 9, *)
    func fillWidthOfSuperview(margin: CGFloat)

    Parameters

    margin

    A CGFloat representing the horizontal margin.

  • Pin the view’s vertical edges to the top and bottom of its superview with a margin.

    Author

    Daniel Loewenherz

    Date

    February 17, 2016

    Declaration

    Swift

    @available(iOS 9, *)
    func fillHeightOfSuperview(margin: CGFloat)

    Parameters

    margin

    A CGFloat representing the vertical margin.

  • Pin the view’s edges to the top, bottom, left, and right of its superview with a margin.

    Author

    Daniel Loewenherz

    Date

    February 17, 2016

    Declaration

    Swift

    @available(iOS 9, *)
    func fillSuperview(_ axis: NSLayoutConstraint.Axis? = nil, margin: CGFloat = 0)

    Parameters

    axis

    Optional. Specify only when you want to pin the view to a particular axis.

    margin

    A CGFloat representing the vertical margin.

  • Apply the specified visual format constraints to the current view. The “view” placeholder is used in the provided VFL string. Usage can best be illustrated through examples:

    Pin a UIView to the left and right of its superview with a 20px margin:

    let view = UIView()
    view.addVisualFormatConstraints("|-20-[view]-20-|")
    

    Set a view to be at least 36px high, and at least 16px from the top of its superview.

    view.addVisualFormatConstraints("V:|-(>=16)-[view(>36)]")
    

    Author

    Daniel Loewenherz

    Date

    February 17, 2016

    Declaration

    Swift

    @discardableResult
    func addVisualFormatConstraints(_ format: String, metrics: LayoutDictionary? = nil) -> [NSLayoutConstraint]

    Parameters

    format

    The format specification for the constraints. For more information, see Visual Format Language in Auto Layout Guide.

  • Apply the VFL format constraints to the specified views, with the provided metrics.

    Author

    Daniel Loewenherz

    Date

    February 17, 2016

    Declaration

    Swift

    @discardableResult
    class func addVisualFormatConstraints(_ format: String, metrics: LayoutDictionary? = nil, views: LayoutDictionary) -> [NSLayoutConstraint]

    Parameters

    format

    The format specification for the constraints. For more information, see Visual Format Language in Auto Layout Guide.

    metrics

    A dictionary of constants that appear in the visual format string. The dictionary’s keys must be the string values used in the visual format string. Their values must be NSNumber objects.

    views

    A dictionary of views that appear in the visual format string. The keys must be the string values used in the visual format string, and the values must be the view objects.

  • Return the given distance from a view to a specified CGPoint.

    Author

    Daniel Loewenherz

    Date

    February 17, 2016

    Declaration

    Swift

    func distance(toPoint point: CGPoint) -> Float

    Parameters

    point

    The CGPoint to measure the distance to.

    Return Value

    A Float representing the distance to the specified CGPoint.

  • Returns a 1x1 CGRect in the center of the calling UIView.

    Author

    Daniel Loewenherz

    Date

    March 9, 2016

    Declaration

    Swift

    var centerRect: CGRect { get }

    Return Value

    A 1x1 CGRect.

  • Return an Array of recursive subviews matching the provided test. This method is incredibly helpful in returning all descendants of a given view of a certain type. For instance:

    let view = UIView()
    let subview = UILabel()
    let label1 = UILabel()
    let label2 = UILabel()
    
    view.addSubview(subview)
    view.addSubview(label1)
    subview.addSubview(label2)
    
    let labels: [UILabel] = view.descendantViewsOfType()
    

    labels will contain both label1 and label2.

    Author

    Daniel Loewenherz

    Date

    March 9, 2016

    Declaration

    Swift

    func descendantViewsOfType<T>(passingTest test: (T) -> Bool = { _ in true }) -> [T]

    Return Value

    An Array of UIViews of type T.

  • This method simply returns the last object in descendantViewsOfType(passingTest:).

    Author

    Daniel Loewenherz

    Date

    March 9, 2016

    Declaration

    Swift

    func lastDescendantViewOfType<T>(passingTest test: (T) -> Bool = { i in true }) -> T?

    Return Value

    A view of type T.