UIView

public extension UIView

A set of extensions to the UIView class. It has a few “informational” computed properties, and some other simple tools for things like auto layout.

IBInspectable Computed Instance Properties

  • This gives us access to the corner radius, so we can give the view rounded corners.

    This requires that clipsToBounds be set.

    Declaration

    Swift

    @IBInspectable
    var cornerRadius: CGFloat { get set }
  • Inspired by this SO answer This allows us to specify a border for the view. It is width, in display units.

    Declaration

    Swift

    @IBInspectable
    var borderWidth: CGFloat { get set }
  • Inspired by this SO answer This allows us to assign a color to any border that is of a width greater than 0 display units.

    Declaration

    Swift

    @IBInspectable
    var borderColor: UIColor? { get set }

Responder Stuff

  • This returns the first responder, wherever it is in our hierarchy.

    Declaration

    Swift

    var currentFirstResponder: UIResponder? { get }
  • This puts away any open keyboards.

    Declaration

    Swift

    func resignAllFirstResponders()

Auto Layout Instance Methods

  • This allows us to add a subview, and set it up with auto-layout constraints to fill the superview.

    Declaration

    Swift

    @discardableResult
    func addContainedView(_ inSubView: UIView, underThis inUpperConstraint: NSLayoutYAxisAnchor? = nil, andGiveMeABottomHook inBottomLoose: Bool = false) -> NSLayoutYAxisAnchor?

    Parameters

    inSubview

    The subview we want to add.

    underThis

    If supplied, this is a Y-axis anchor to use as the attachment of the top anchor. Default is nil (can be omitted, which will simply attach to the top of the container).

    andGiveMeABottomHook

    If this is true, then the bottom anchor of the subview will not be attached to anything, and will simply be returned. Default is false, which means that the bottom anchor will simply be attached to the bottom of the view.

    Return Value

    The bottom hook, if requested. Can be ignored.

  • This creates a constraint, locking the view to a given aspect ratio.

    Declaration

    Swift

    func autoLayoutAspectConstraint(aspectRatio inAspect: CGFloat) -> NSLayoutConstraint?

    Parameters

    aspectRatio

    The aspect ratio. It is W/H, so numbers less than 1.0 are wider than tall, and numbers greater than 1.0 are taller than wide.

    Return Value

    An inactive constraint, locking this view to the given aspect ratio.