UIView

extension UIView

We add a couple of ways to deal with first responders.

  • 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 }
  • 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, priority inPriority: UILayoutPriority = .required, constant inConstant: CGFloat = 0) -> 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.

    priority

    The priority. This is optional, and default is .required

    constant

    This is the constant to be applied. This is optional, and default is 0.

    Return Value

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