AFViewHelper 4.1.0

AFViewHelper 4.1.0

TestsTested
LangLanguage SwiftSwift
License MIT
ReleasedLast Release Jul 2016
SPMSupports SPM

Maintained by Melvin Rivera.



  • By
  • Melvin Rivera

AFViewHelper

Autolayout and Animation UIVIew Extension for Swift 2.0

Sample Project Screenshot

Animations

// Required
view.animate(.Shake, curve: .EaseInOutBack)
// With optionals
view.animate(.Shake, curve: .EaseInOutBack, duration: 0.8, delay: 0, force: 1, damping: 0.7,velocity: 1, fromRotation: 0, fromScale: 1.5, fromX: 0, fromY: 0)

Auto Layout

let box = UIView(autoLayout:true)
view.addSubview(box)
box.backgroundColor = UIColor.redColor()
box.width(100)
box.height(100)
box.center(to: view)
view.layoutIfNeeded()

Chainable functions

100 pixel view pinned to the center of another view

view.width(100).height(100).center(to: view)

Compression & Hugging

view.horizontalCompressionPriority(UILayoutPriorityDefaultHigh)
view.horizontalHuggingPriority(UILayoutPriorityDefaultLow)

NSLayoutConstraints

You can have access to the NSLayoutConstraints by using pin() or applyAttribute.

// Pinning to an item
let widthConstraint = view.pin(.Width, to: view, attribute: .Height, constant: 0, multiplier: 0.5, relation: .LessThanOrEqual)
// Applying an attribute
let widthConstraint =  view.applyAttribute(.Width, constant: 100, multiplier: 0.5, relation: .Equal)

UIView Extension

Prepping for Auto Layout

If a view is already in place with frames, prepForAutoLayout() will remove it from view, enable Auto Layout and place back in view.

view.prepForAutoLayout()

Prepping for Animation

If a view uses auto layout but you need to animate it using frames, prepForAnimation() will remove from view, disable auto layout and place back in view.

view.prepForAnimation()

Instantiate

Instantiates a new UIView with Auto Layout

convenience init(autoLayout: Bool = true)

Position

Origin

Returns the frame’s origin

origin() -> CGPoint

Pins the frame’s top and left sides using Auto Layout or frames

origin(constant: CGPoint) -> UIView 

Pins left and top sides to another view using Auto Layout

origin(to to:AnyObject, constant: CGPoint = CGPoint(x: 0, y: 0), multiplier:CGFloat = 1) -> UIView

Left

Returns the min x point

left() -> CGFloat 

Pins the left side using Auto Layout or frames

left(constant: CGFloat) -> UIView

Pins left side to another view using Auto Layout

left(to to:AnyObject, attribute: NSLayoutAttribute = .Left, constant: CGFloat = 0, multiplier:CGFloat = 1) -> UIView

Leading

Returns the leading side value

leading() -> CGFloat

Pins the leading side using Auto Layout or frames

leading(constant: CGFloat) -> UIView

Pins the leading side to another view using Auto Layout

leading(to to:AnyObject, attribute: NSLayoutAttribute = .Leading, constant: CGFloat = 0, multiplier:CGFloat = 1) -> UIView

Right

Returns the max x point

right() -> CGFloat

Pins the right side using Auto Layout or frames

right(constant: CGFloat) -> UIView 

Pins the right side to another view using Auto Layout

right(to to:AnyObject, attribute: NSLayoutAttribute = .Right, constant: CGFloat = 0, multiplier:CGFloat = 1) -> UIView

Trailing

Returns the trailing side value

trailing() -> CGFloat

Pins the trailing side using Auto Layout or frames

trailing(constant: CGFloat) -> UIView 

Pins the trailing side to another view using Auto Layout

trailing(to to:AnyObject, attribute: NSLayoutAttribute = .Trailing, constant: CGFloat = 0, multiplier:CGFloat = 1) -> UIView

Top

Returns the top side value

top() -> CGFloat 

Pins the top side using Auto Layout or frames

top(constant: CGFloat) -> UIView

Pins the trailing side to another view using Auto Layout

top(to to:AnyObject, attribute: NSLayoutAttribute = .Top, constant: CGFloat = 0, multiplier:CGFloat = 1) -> UIView

Bottom

Returns the bottom side value

bottom() -> CGFloat

Pins the bottom side using Auto Layout or frames

bottom(constant: CGFloat) -> UIView 

Pins the bottom side to another view using Auto Layout

bottom(to to:AnyObject, attribute: NSLayoutAttribute = .Bottom, constant: CGFloat = 0, multiplier:CGFloat = 1) -> UIView 

Center

Pins the center to it’s superview using Auto Layout or frames

center(constant: CGPoint = CGPoint(x: 0, y: 0)) -> UIView

Pins the center point to another view using Auto Layout

center(to to:AnyObject, constant:CGSize = CGSize(width: 0, height: 0), multiplier:CGFloat = 1) -> UIView 

Center X

Returns the center X

centerX() -> CGFloat

Pins the center X using Auto Layout or frames

centerX(constant: CGFloat = 0) -> UIView 

Pins the center X to another view using Auto Layout

centerX(to to:AnyObject, attribute: NSLayoutAttribute = .CenterX, constant: CGFloat = 0, multiplier:CGFloat = 1) -> UIView

Center Y

Returns the center Y

centerY() -> CGFloat

Pins the center Y using Auto Layout or frames

centerY(constant: CGFloat = 0) -> UIView

Pins the center Y to another view using Auto Layout

centerY(to to:AnyObject, attribute: NSLayoutAttribute = .CenterY, constant: CGFloat = 0, multiplier:CGFloat = 1) -> UIView

Compression and Hugging Priority

Compression Priority

Returns the Compression Resistance Priority for Horizontal Axis using Auto Layout

horizontalCompressionPriority() -> UILayoutPriority

Sets the Compression Resistance Priority for Horizontal Axis using Auto Layout

horizontalCompressionPriority(priority: UILayoutPriority) -> UIView  

Returns the Compression Resistance Priority for Vertical Axis using Auto Layout

verticalCompressionPriority() -> UILayoutPriority

Sets the Compression Resistance Priority for Vertical Axis using Auto Layout

verticalCompressionPriority(priority: UILayoutPriority) -> UIView

Hugging Priority

Returns the Content Hugging Priority for Horizontal Axis using Auto Layout

func horizontalHuggingPriority() -> UILayoutPriority

Sets the Content Hugging Priority for Horizontal Axis using Auto Layout

horizontalHuggingPriority(priority: UILayoutPriority) -> UIView

Returns the Content Hugging Priority for Vertical Axis using Auto Layout

verticalHuggingPriority() -> UILayoutPriority

Sets the Content Hugging Priority for Vertical Axis using Auto Layout

verticalHuggingPriority(priority: UILayoutPriority) -> UIView

Size

Size

Returns the frame size

size() -> CGSize

Sets the frame size using Auto Layout or frames

size(constant: CGSize) -> UIView

Pins the size to another view using Auto Layout

size(to to:AnyObject, constant: CGSize = CGSize(width: 0, height: 0), multiplier:CGFloat = 1) -> UIView

Width

Returns the frame width

width() -> CGFloat 

Sets the frame width using Auto Layout or frames

width(constant: CGFloat) -> UIView

Pins the width to another view using Auto Layout

width(to to:AnyObject, attribute: NSLayoutAttribute = .Width, constant: CGFloat = 0, multiplier:CGFloat = 1) -> UIView

Height

Returns the frame height

height() -> CGFloat

Sets the frame height using Auto Layout or frames

eight(constant: CGFloat) -> UIView

Pins the height to another view using Auto Layout

height(to to:AnyObject, attribute: NSLayoutAttribute = .Height, constant: CGFloat = 0, multiplier:CGFloat = 1) -> UIView

Minimum Size

Returns the minimum size using Auto Layout

minSize() -> CGSize?

Sets the minimum size using Auto Layout

minSize(constant:CGSize) -> UIView

Pins the minimum size to another view using Auto Layout

minSize(to to:AnyObject, constant:CGSize = CGSize(width: 0, height: 0), multiplier:CGFloat = 1)

Minimum Width

Returns the minimum width using Auto Layout

minWidth() -> CGFloat?

Sets the minimum width using Auto Layout

minWidth(constant:CGFloat) -> UIView

Pins the minimum width to another view using Auto Layout

minWidth(to to:AnyObject, attribute: NSLayoutAttribute = .Width, constant:CGFloat = 0, multiplier:CGFloat = 1) -> UIView

Minimum Height

Returns the minimum height using Auto Layout

minHeight() -> CGFloat?

Sets the minimum height using Auto Layout

minHeight(constant:CGFloat) -> UIView 

Pins the minimum height to another view using Auto Layout

minHeight(to to:AnyObject, attribute: NSLayoutAttribute = .Height, constant:CGFloat = 0, multiplier:CGFloat = 1) -> UIView

Maximum Size

Returns the maximun size using Auto Layout

maxSize() -> CGSize?

Sets the maximun size using Auto Layout

maxSize(constant:CGSize) -> UIView

Pins the maximun size to another view using Auto Layout

maxSize(to to:AnyObject, constant:CGSize = CGSize(width: 0, height: 0), multiplier:CGFloat = 1) -> UIView

Maximum Width

Returns the maximun width using Auto Layout

maxWidth() -> CGFloat?

Sets the maximun width using Auto Layout

maxWidth(constant:CGFloat) -> UIView

Pins the maximun width to another view using Auto Layout

maxWidth(to to:AnyObject, attribute: NSLayoutAttribute = .Width, constant:CGFloat = 0, multiplier:CGFloat = 1) -> UIView

Maximum Height

Returns the maximun height using Auto Layout

maxHeight() -> CGFloat?

Sets the maximun height using Auto Layout

maxHeight(constant:CGFloat) -> UIView

Pins the maximun height to another view using Auto Layout

maxHeight(to to:AnyObject, attribute: NSLayoutAttribute = .Height, constant:CGFloat = 0, multiplier:CGFloat = 1) -> UIView

Smallest and Largest Size

Returns the length of the smallest side

smallestSideLength() -> CGFloat

Returns the length of the largest side

largestSideLength() -> CGFloat

AutoLayout state

Prepares the view for a frame based animation by removing the view, enabling translatesAutoresizingMaskIntoConstraints and re-adding the view to it’s superview

prepForAnimation()

Prepares the view for Auto Layout by removing the view, disabling translatesAutoresizingMaskIntoConstraints and re-adding the view to it’s superview

prepForAutoLayout()

Pin and Apply

Pins an attribute to another view

pin(pinAttribute:NSLayoutAttribute, to:AnyObject? = nil, attribute:NSLayoutAttribute, constant:CGFloat = 0, multiplier:CGFloat = 1, relation:NSLayoutRelation = .Equal) -> NSLayoutConstraint? 

Applies an attribute to the view

applyAttribute(attribute:NSLayoutAttribute, constant:CGFloat = 0, multiplier: CGFloat = 1, relation:NSLayoutRelation = .Equal) -> NSLayoutConstraint

Removing Constraints

Removes all attached constraints recursevely

removeAttachedConstraintsRecursevely() -> UIView

Removes a constraint recursevely

removeConstraintRecursevely(constraint:NSLayoutConstraint) -> UIView

Direction

Returns true if layout direction is left to right

layoutDirectionIsLeftToRight() -> Bool

UIView Effects Extension

Border

The layer border color

var borderColor: UIColor 

The layer border width

var borderWidth: CGFloat

Sets layer border with a dash pattern

borderWithDashPattern(lineDashPattern: [Int], borderWidth: CGFloat, borderColor: UIColor, cornerRadius: CGFloat?) -> UIView 

Rounded Corners

The layer corner radius

var cornerRadius: CGFloat 

Creates a circle by rounding the corners to half the size of the width, sets border color and width

roundCornersToCircle(borderColor: UIColor?, borderWidth: CGFloat?) -> UIView 

Creates a circle by rounding the corners to hald the size of the width

roundCorners(cornerRadius: CGFloat, borderColor: UIColor?, borderWidth: CGFloat?) -> UIView

Shadow

The shadow color of the layer

var shadowColor: UIColor 

The shadow offset of the layer

var shadowOffset:CGSize

The shadow opacity of the layer

var shadowOpacity:Float

The shadow radius of the layer

var shadowRadius:CGFloat

Sets shadow of the layer including the color, offset, radius, opacity and mask.

shadow(color: UIColor = UIColor.blackColor(), offset: CGSize = CGSize(width: 0, height: 0), radius: CGFloat = 6, opacity: Float = 1, isMasked: Bool = false) -> UIView

Gradient

Sets a gradient color layer

setGradient(colors: [UIColor], isHorizontal:Bool = false) -> UIView

Animates colors of a gradient layer

animateGradientToColors(colors: [UIColor], duration: CFTimeInterval = 3) -> UIView

Sets a gradient layer mask

setGradientMask(alphas:[CGFloat], isHorizontal:Bool = false) -> UIView

UIViewController Extension

Direction

Returns true if layout direction is left to right

layoutDirectionIsLeftToRight() -> Bool

Returns true if horizontal size class is compact

horizontalSizeClassIsCompact() -> Bool

Returns true if vertical size class is compact

verticalSizeClassIsCompact() -> Bool