Brandon Withrow, brandon@withrow.io
UIToolBox is available under the MIT license. See the LICENSE file for more info.
public struct ConstraintGroupA group of NSLayoutConstraints. This class provides quick and easy modification to a group of constraints. Typically used for AutoLayout based animations.
public let constraints: [NSLayoutConstraint]public init(constraints: [NSLayoutConstraint])Initialize with a group of constraints.
public func enable()Enables all constraints in the group.
public func disable()Disables all constraints in the group.
public func setConstant(constant: CGFloat)Sets the constant on all constraints in the group.
public func animateLayoutChanges(style: UIViewAnimationStyle,
completion: ((Bool) -> Void)?)Auto animates the layout changes of a view using a set style.
public class UIViewAnimationStyleDescribes a basic animation.
public var type: LayoutAnimationType = .easeInpublic var speed: Double = 0.4public enum LayoutAnimationTypeEnum that describes a basic animation type.
case easeInAn ease-in curve causes the animation to begin slowly, and then speed up as it progresses.
case easeOutAn ease-out curve causes the animation to begin quickly, and then slows down as it progresses.
case easeInOutSpecify an ease-in ease-out curve, which causes the animation to begin slowly, accelerate through the middle of its duration, and then slow again before completing.
case springA spring based animation curve that overshoots and rests.
@discardableResult public func pinToSuperview(padding: CGFloat = 0) -> ConstraintGroup?Pin view to its superview and returns a ConstraintGroup containing the constraints. Returns nil if superview is nil.
@discardableResult public func pinToSuperviewMargins(padding: CGFloat = 0) -> ConstraintGroup?Pin view to superview margins and returns a ConstraintGroup containing the constraints. Returns nil if superview is nil.
@discardableResult public func pinToSafeArea() -> ConstraintGroup?Pin view to Safe Area and returns a ConstraintGroup containing the constraints. Returns nil if superview is nil.
@discardableResult public func pinToBottomSafeArea(constant: CGFloat) -> NSLayoutConstraint?Pins the bottom of the view to the superview's safe area. Returns nil if superview is nil.
@discardableResult public func pinToBottomSafeArea(safeAreaPadding:CGFloat = 0, maxPadding: CGFloat) -> ConstraintGroup?Pins the bottom of the view to the superview's safe area with a max padding from the bottomAnchor.
This is helpful for pinning a view, such as a button, to the bottom of a view so that the spacing is consistent on both iPhone X and iPhone 6-8. The button will be pinned to the safe area on iPhone X, or it will be above the bottomAnchor on iPhone 6-8
let button = UIButton() view.addSubview(button) button.pinToLeadingAndTrailing() button.pinToBottomSafeArea(0, maxPadding: 20) // Result: The button will be pinned to the safe area on iPhone X, or it will be pinned 20 points above the bottomAnchor on iPhone 8
- Parameter safeAreaPadding: The amount of padding to place the view above the safeArea.
- Parameter maxPadding: The amount of padding to plae the view above the bottomAnchor.
| Name | Description |
|---|---|
| safeAreaPadding | The amount of padding to place the view above the safeArea. |
| maxPadding | The amount of padding to plae the view above the bottomAnchor. |
@discardableResult public func pinToCenter() -> ConstraintGroup?Pin view to superview center.
@discardableResult public func pinToHorizontalCenter(constant: CGFloat = 0) -> NSLayoutConstraint?Pins the view to the horizontal center of its superview.
@discardableResult public func pinToVerticalCenter(constant: CGFloat = 0) -> NSLayoutConstraint?Pins the view to the vertical center of its superview.
@discardableResult public func pinToVerticalCenterOrFit(constant: CGFloat = 0) -> ConstraintGroup?Pins the view to the vertical center of its superview. The top and bottom anchor are also pinned to be less than and greater to the superview's. This results in a view that fits its superview depending on the superview's content.
@discardableResult public func pinToHorizontalCenterOrFit(constant: CGFloat = 0) -> ConstraintGroup?Pins the view to the horizontal center of its superview. The leanding and trailing anchor are also pinned to be less than and greater to the superview's. This results in a view that fits its superview depending on the superview's content.
@discardableResult public func pinLeadingAndTrailingToMargins(constant: CGFloat = 0) -> ConstraintGroup?Pin leading and trailing to superview margins.
@discardableResult public func pinToLeadingAndTrailing(constant: CGFloat = 0) -> ConstraintGroup?Pin leading and trailing to superview.
@discardableResult public func pinMinimumToLeadingAndTrailing(constant: CGFloat = 0) -> ConstraintGroup?Pins leading and trailing to be greater than or less than the superview's leading and trailing
@discardableResult public func pinToLeading(constant: CGFloat = 0) -> NSLayoutConstraint?Pin leading to superview leading
@discardableResult public func pinToTrailing(constant: CGFloat = 0) -> NSLayoutConstraint?Pin trailing to superview trailing
@discardableResult public func pinLeadingToMargin(constant: CGFloat = 0) -> NSLayoutConstraint?Pin leading to superview leading margin
@discardableResult public func pinTrailingToMargin(constant: CGFloat = 0) -> NSLayoutConstraint?Pin trailing to superview trailing margin
@discardableResult public func pinBottomToMargin(constant: CGFloat = 0) -> NSLayoutConstraint?Pin top to superview's top margin
@discardableResult public func pinTopToMargin(constant: CGFloat = 0) -> NSLayoutConstraint?Pin bottom to superview's bottom margin
@discardableResult public func pinToTopAndBottom(constant: CGFloat = 0) -> ConstraintGroup?Pin view to top and bottom of superview
@discardableResult public func pinTopAndBottomToMargin(constant: CGFloat = 0) -> ConstraintGroup?Pin top and bottom to margin of superview
@discardableResult public func pinToTop(constant: CGFloat = 0) -> NSLayoutConstraint?Pin view to top of superview
@discardableResult public func pinToBottom(constant: CGFloat = 0) -> NSLayoutConstraint?Pin view to bottom of superview
@discardableResult public func pinLessThanOrEqualToTrailing(constant: CGFloat = 0) -> NSLayoutConstraint?Pin trailingAnchor to less than or equal to superview's trailingAnchor
@discardableResult public func pinGreaterThanOrEqualToTrailing(constant: CGFloat = 0) -> NSLayoutConstraint?Pin leading to less than or equal to superview's leadingAnchor
@discardableResult public func pinHorizontalCenterToView(_ view: UIView, constant: CGFloat = 0) -> NSLayoutConstraintPin reciever to target views horizontal center
@discardableResult public func pinVerticalCenterToView(_ view: UIView, constant: CGFloat = 0) -> NSLayoutConstraintPin reciever to target views vertical center
@discardableResult public func alignTopToView(_ view: UIView, constant: CGFloat = 0) -> NSLayoutConstraintAlign the topAnchor with the topAnchor of the given view.
@discardableResult public func alignBottomToView(_ view: UIView, constant: CGFloat = 0) -> NSLayoutConstraintAlign the bottomAnchor with the bottomAnchor of the given view.
@discardableResult public func alignLeadingToView(_ view: UIView, constant: CGFloat = 0) -> NSLayoutConstraintAlign leadingAnchor with the leadingAnchor of the given view.
@discardableResult public func alignTrailingToView(_ view: UIView, constant: CGFloat = 0) -> NSLayoutConstraintAlign trailingAnchor with the trailingAnchor of the given view.
@discardableResult public func alignWidthToView(_ view: UIView, constant: CGFloat = 0, multiplier: CGFloat = 1) -> NSLayoutConstraintAlign the width to the given view
@discardableResult public func alignHeightToView(_ view: UIView, constant: CGFloat = 0, multiplier: CGFloat = 1) -> NSLayoutConstraintAlign the width to the given view
@discardableResult public func alignToView(_ view: UIView, constant: CGFloat = 0) -> ConstraintGroupAlign top, bottom, leading, and trailing to given view.
@discardableResult public func pinBelowView(_ view: UIView, constant: CGFloat = 0) -> NSLayoutConstraintPin below given view. Constrains the topAnchor to the bottomAnchor of the given view.
@discardableResult public func pinAboveView(_ view: UIView, constant: CGFloat = 0) -> NSLayoutConstraintPin above given view. Constrains the bottomAnchor to the topAnchor of the given view.
@discardableResult public func pinLeadingToView(_ view: UIView, constant: CGFloat = 0) -> NSLayoutConstraintPin right to given view (Or left in RTL layouts). Constrains leadingAnchor to trailingAnchor of given view.
@discardableResult public func pinLeadingGreaterThanOrEqualToView(_ view: UIView, constant: CGFloat = 0) -> NSLayoutConstraintPin leadingAnchor to be greater than or equal to trailingAnchor of given view.
@discardableResult public func pinTrailingToView(_ view: UIView, constant: CGFloat = 0) -> NSLayoutConstraintPin left to given view (Or right in RTL layouts). Constrains trailingAnchor to leadingAnchor of given view.
@discardableResult public func pinTrailingLessThanOrEqualToView(_ view: UIView, constant: CGFloat = 0) -> NSLayoutConstraintPin trailingAnchor to be less than or equal to leadingAnchor of given view.
@discardableResult public func constrainWidth(_ width: CGFloat) -> NSLayoutConstraintConstrains the view's width
@discardableResult public func constrainHeight(_ height: CGFloat) -> NSLayoutConstraintConstrains the view's height
@discardableResult public func constrainSize(_ size: CGSize) -> ConstraintGroupConstrains the view's size
public func setMargins(top: CGFloat = 0, leading: CGFloat = 0, bottom: CGFloat = 0, trailing: CGFloat = 0)Sets the layout margins of the view. Convenience safe to use in ios 10+. In iOS 10- this sets layoutMargins. In iOS 11+ it sets directionalLayoutMargins
public func interpolate(_ toNumber: Double, amount: Double) -> DoubleInterpolates the reciever to the given number by Amount.
- Parameter toNumber: The number to interpolate to.
- Parameter amount: The amount to interpolate from 0-1
let number = 5 let interpolated = number.interpolate(10, amount: 0.5) print(interpolated) // Result: 7.5
- The amount can be greater than one and less than zero. The interpolation will not be clipped.
| Name | Description |
|---|---|
| toNumber | The number to interpolate to. |
| amount | The amount to interpolate from 0-1 |
public func clamp(_ a: Double, _ b: Double) -> DoubleReturns a value that is clamped between the two numbers
- The order of arguments does not matter.
public func arrayBySubdividing(by: Int) -> Array<Array>Returns an array that contains the elements of the receiving array nested in child arrays by stride.
public func shift(withDistance distance: Int = 1) -> Array<Element>Returns an array by shifting the elements of the receiving array by distance.
public mutating func shiftInPlace(withDistance distance: Int = 1)Shifts the array in place by distance.
public func interpolate(_ toNumber: CGFloat, amount: CGFloat) -> CGFloatInterpolates the reciever to the given number by Amount.
- Parameter toNumber: The number to interpolate to.
- Parameter amount: The amount to interpolate from 0-1
let number = 5 let interpolated = number.interpolate(10, amount: 0.5) print(interpolated) // Result: 7.5
- The amount can be greater than one and less than zero. The interpolation will not be clipped.
| Name | Description |
|---|---|
| toNumber | The number to interpolate to. |
| amount | The amount to interpolate from 0-1 |
public func clamp(_ a: CGFloat, _ b: CGFloat) -> CGFloatReturns a value that is clamped between the two numbers
- The order of arguments does not matter.
public func diff(_ a: CGFloat, absolute: Bool = true) -> CGFloatReturns the difference between the receiver and the given number.
- Parameter absolute: If true (Default) the returned value will always be positive.
| Name | Description |
|---|---|
| absolute | If (Default) the returned value will always be positive. |
public func toRadians() -> CGFloatpublic func toDegrees() -> CGFloatpublic var vectorLength: CGFloatReturns the length between the reciever and CGPoint.zero
public func distanceFrom(_ a: CGPoint) -> CGFloatReturns the distance between the reciever and the given point.
public func rounded(decimal: CGFloat) -> CGPointpublic func interpolate(_ to: CGPoint, amount: CGFloat) -> CGPointInterpolates the reciever to the given Point by Amount.
- Parameter to: The Point to interpolate to.
- Parameter amount: The amount to interpolate from 0-1
let point = CGPoint(width: 50, height: 50) let interpolated = rect.interpolate(CGPoint(width: 100, height: 100), amount: 0.5) print(interpolated) // Result: (x: 75, y: 75)
- The amount can be greater than one and less than zero. The interpolation will not be clipped.
| Name | Description |
|---|---|
| to | The Point to interpolate to. |
| amount | The amount to interpolate from 0-1 |
public func subtract(_ point: CGPoint) -> CGPointSubtracts the given point from the receiving point.
public func add(_ point: CGPoint) -> CGPointAdds the given point from the receiving point.
public static func / (lhs: CGPoint, rhs: Float) -> CGPointOperator convenience to divide points with /
public static func * (lhs: CGPoint, rhs: Float) -> CGPointOperator convenience to multiply points with *
public static func +(left: CGPoint, right: CGPoint) -> CGPointOperator convenience to add points with +
public static func -(left: CGPoint, right: CGPoint) -> CGPointOperator convenience to subtract points with -
public func interpolate(_ to: CGSize, amount: CGFloat) -> CGSizeInterpolates the reciever to the given size by Amount.
- Parameter to: The size to interpolate to.
- Parameter amount: The amount to interpolate from 0-1
let size = CGSize(width: 50, height: 50) let interpolated = rect.interpolate(CGSize(width: 100, height: 100), amount: 0.5) print(interpolated) // Result: (width: 75, height: 75)
- The amount can be greater than one and less than zero. The interpolation will not be clipped.
| Name | Description |
|---|---|
| to | The size to interpolate to. |
| amount | The amount to interpolate from 0-1 |
public func scaleThatFits(_ size: CGSize) -> CGFloatReturns the scale float that will fit the recieve inside of the given size.
public func add(_ size: CGSize) -> CGSizeAdds reciever size to give size.
public func subtract(_ size: CGSize) -> CGSizeSubtracts given size from reciever size.
public func multiply(_ size: CGSize) -> CGSizeMultiplies reciever size by the given size.
public static func +(left: CGSize, right: CGSize) -> CGSizeOperator convenience to add sizes with +
public static func -(left: CGSize, right: CGSize) -> CGSizeOperator convenience to subtract sizes with -
public static func *(left: CGSize, right: CGFloat) -> CGSizeOperator convenience to multiply sizes with *
public var area: CGFloatReturns the total area of the rect.
public var center: CGPointThe center point of the rect. Settable.
public var topLeft: CGPointThe top left point of the rect. Settable.
public var bottomLeft: CGPointThe bottom left point of the rect. Settable.
public var topRight: CGPointThe top right point of the rect. Settable.
public var bottomRight: CGPointThe bottom right point of the rect. Settable.
public init(center: CGPoint, size: CGSize)Initializes a new CGRect with a center point and size.
public func interpolate(_ to: CGRect, amount: CGFloat) -> CGRectInterpolates the reciever to the given rect by Amount.
- Parameter to: The rect to interpolate to.
- Parameter amount: The amount to interpolate from 0-1
let rect = CGRect(x:0, y:0, width: 50, height: 50) let interpolated = rect.interpolate(CGRect(x:100, y:100, width: 100, height: 100), amount: 0.5) print(interpolated) // Result: (x: 50, y: 50, width: 75, height: 75)
- The amount can be greater than one and less than zero. The interpolation will not be clipped.
| Name | Description |
|---|---|
| to | The rect to interpolate to. |
| amount | The amount to interpolate from 0-1 |
public struct CGLineA struct that defines a line segment with two CGPoints
public var start: CGPointThe Start of the line segment.
public var end: CGPointThe End of the line segment.
public var length: CGFloatThe lenght of the line segment.
public init(start: CGPoint, end: CGPoint)Initializes a line segment with start and end points
public func normalize() -> CGLineReturns a line sgement that is normalized to a length of 1
public func trimmedToLength(_ tolength: CGFloat) -> CGLineTrims a line segment to the given length
public func flipped() -> CGLineFlips a line vertically and horizontally from the start point.
public func transpose(_ toPoint: CGPoint) -> CGLineMove the line to the new start point.
public convenience init(backgroundColor: UIColor?)Initializes a view with a background color.
public func setShadow(shadow: LayerShadow?)Getter/Setter for the layer's corner radius.
public struct LayerShadowSets a LayerShadow on a UIView
public var cornerRadius: CGFloatA struct that wraps shadow styles.
public let shadowColor: UIColorpublic let shadowOpacity: Floatpublic let shadowRadius: CGFloatpublic let shadowOffset: CGSizepublic init(shadowColor: UIColor,
shadowOpacity: Float,
shadowRadius: CGFloat,
shadowOffset: CGSize)public var darker: UIColorDarkens the color by 20 percent
public var lighter: UIColorLightens the color by 20 percent
public var complementary: UIColorReturns the complementary color of the reciever
public var triadic: (left: UIColor, right: UIColor)Returns triadic color pairs as Left and Right
public var analagous: (left: UIColor, right: UIColor)Returns analagous color pairs as Left and Right
public var hue: CGFloatReturns the Hue of the color
public var saturation: CGFloatReturns the saturation of the color
public var brightness: CGFloatreturns the brightness of the color
public var luminance: CGFloatReturns the Accesibility luminance of a color
public var red: CGFloatReturns the Red channel of the color
public var green: CGFloatReturns the Green channel of the color
public var blue: CGFloatReturns the Blue channel of the color
public var alpha: CGFloatReturns the Alpha channel of the color
var hsba:(h: CGFloat, s: CGFloat, b: CGFloat, a: CGFloat)Returns color components in an Hue/Saturation/Brightness color space
var rgba:(r: CGFloat, g: CGFloat, b: CGFloat, a: CGFloat)Returns color components in an Red/Green/Blue color space
var yuva:(y: CGFloat, u: CGFloat, v: CGFloat, a: CGFloat)Returns color components in an Y'UV Color Space.
public convenience init(hexString: String)Initializes a string with a hex value
public convenience init(y: CGFloat, u: CGFloat, v: CGFloat, alpha: CGFloat)Initializes a new color with YUV coordinates.
public func interpolateToColor(_ toColor: UIColor, amount: CGFloat, coordinateSpace: ColorCoordinateSpace = .hsb) -> UIColorInterpolates from the recieving color to the given color by an amount from 0-1, You can provide an optional Color coordinate space to perform the interpolation.
- The return value is not clipped, values greater than 1 may blow out color.
- The different color coordinate spaces will create different color interpolations. HSB generally creates the most pleasing results. YUV and RGB will often pass through grey if the colors are complementary.
public func withBlack(black: CGFloat) -> UIColorReturns a new color that mixes the receiving color with black
public func withWhite(white: CGFloat) -> UIColorReturns a new color that mixes the receiving color with white
public func withHue(hue: CGFloat) -> UIColorReturns a new color created by setting the Hue on the recieving color
public func withHueOffset(_ offset: CGFloat) -> UIColorReturns a new color created by offsetting the Hue on the recieving color
public func withSaturation(saturation: CGFloat) -> UIColorReturns a new color created by setting the Saturation on the recieving color
public func withSaturationOffset(_ offset: CGFloat) -> UIColorReturns a new color created by multiplying the saturation by the offset
public func withBrightness(brightness: CGFloat) -> UIColorReturns a new color created by setting the Brightness on the recieving color
public func withBrightnessOffset(_ offset: CGFloat) -> UIColorReturns a new color created by multiplying the Brightness by the offset
public func contrastRatio(_ withColor: UIColor) -> CGFloatReturns the Accessibility luminance contrast between the two colors
public func contrastIsAccessible(_ withColor: UIColor) -> BoolReturns true if the contrast between the two colors is accessible
public enum ColorCoordinateSpaceDescribes a color space for a UIColor
case rgbThe Red Green Blue color space.
case hsbThe Hue Saturation Brightness color space.
case yuvA color space that maps color into a 3 dimensional coordinate space.
public convenience init?(simpleSmooth points: [CGPoint])Simple smoothing algorithm
This iterates through the points in the array, drawing cubic bezier from the first to the fourth points, using the second and third as control points.
This takes every third point and moves it so that it is exactly inbetween the points before and after it, which ensures that there is no discontinuity in the first derivative as you join these cubic beziers together.
Note, if, at the end, there are not enough points for a cubic bezier, it will perform a quadratic bezier, or if not enough points for that, a line.
- parameter points: The array of
CGPoint.
| Name | Description |
|---|---|
| points | The array of CGPoint. |
public convenience init?(hermiteInterpolatedPoints points: [CGPoint], closed: Bool)Create smooth UIBezierPath using Hermite Spline
This requires at least two points.
Adapted from https://github.com/jnfisher/ios-curve-interpolation See http://spin.atomicobject.com/2014/05/28/ios-interpolating-points/
parameter hermiteInterpolatedPoints: The array of CGPoint values.
parameter closed: Whether the path should be closed or not
returns: An initialized
UIBezierPath, ornilif an object could not be created for some reason (e.g. not enough points).
| Name | Description |
|---|---|
| hermiteInterpolatedPoints | The array of CGPoint values. |
| closed | Whether the path should be closed or not |
public convenience init?(catmullRomInterpolatedPoints points: [CGPoint], closed: Bool, alpha: Float)Create smooth UIBezierPath using Catmull-Rom Splines
This requires at least four points.
Adapted from https://github.com/jnfisher/ios-curve-interpolation See http://spin.atomicobject.com/2014/05/28/ios-interpolating-points/
parameter catmullRomInterpolatedPoints: The array of CGPoint values.
parameter closed: Whether the path should be closed or not
parameter alpha: The alpha factor to be applied to Catmull-Rom spline.
returns: An initialized
UIBezierPath, ornilif an object could not be created for some reason (e.g. not enough points).
| Name | Description |
|---|---|
| catmullRomInterpolatedPoints | The array of CGPoint values. |
| closed | Whether the path should be closed or not |
| alpha | The alpha factor to be applied to Catmull-Rom spline. |
public convenience init(icon: UIImage, color: UIColor)Returns a button with the given icon tinted to the given color.
public convenience init(target: Any?, action: Selector)Returns a new button with target and action
public convenience init(font: UIFont, textColor: UIColor)Returns a new Label with font and text color set.
public func removeAllArrangedSubviews()Removes all arranged subviews from the stackview.
public var scrollDimension: ScrollDimensionThe Current scrollable dimension
public var currentPage: IntThe current visible page of the scrollview
public var numberOfPages: IntThe number of pages contained by the scrollview
public var pageProgress: CGFloatThe progress of the current page from 0 to 1
public var visibleRect: CGRectThe current visible rect of the scrollview
public enum ScrollDimensioncase widthcase heightpublic func layoutHeaderAndFooter()public func addAutoLayoutHeader(header: UIView)Adds a header to the tableview that is layed out with AutoLayout.
public func addAutoLayoutFooter(footer: UIView)Adds a footer to the tableview that is layed out with AutoLayout.
public func layoutTableHeaderView()public func layoutTableFooterView()