Skip to content

About

Impressive Auto Layout DSL for iOS, tvOS & OSX. & It is written in pure swift.

Resources

Stars

0 stars

Watchers

1 watching

Forks

 
 

Repository files navigation

KVConstraintKit

CI Status Version License Platform

KVConstraintKit is a DSL to make easy & impressive Auto Layout constraints on iOS, tvOS & OSX with Swift

Installation

KVConstraintKit is available through CocoaPods. To install it, simply add the following line to your Podfile:

pod "KVConstraintKit"

Custom Operators

The following types of operators are provided by KVConstraintKit, to add, remove, access and modify constraints.

Operator Meaning
+ to add constraint
- to remove constraint
* to modify multiplier of constraint
~ to modify Priority ( UILayoutPriority ), Relation ( NSLayoutRelation ) & Replace constraint
<- to access constraint by attributes ( eg. NSLayoutAttribute.Height )
+== to add equal relation ( NSLayoutRelation.Equal ) constraint
+>= to add greater than or equal relation ( NSLayoutRelation.GreaterThanOrEqual ) constraint
+<= to add less than or equal relation ( NSLayoutRelation.LessThanOrEqual ) constraint
*== to add equal relation ( NSLayoutRelation.Equal ) constraint with multiplier
*>= to add greater than or equal relation ( NSLayoutRelation.GreaterThanOrEqual ) constraint with multiplier
*<= to add less than or equal relation ( NSLayoutRelation.LessThanOrEqual ) constraint with multiplier

Usage

Here's a quick example:

Without Using KVConstraintKit
let v = UIView.prepareNewViewForAutoLayout()
v.backgroundColor = UIColor.redColor()
view.addSubview(v)
    
// Prepare constraints and then add them on superview of view
let top = NSLayoutConstraint( item: v, attribute: .Top,
	                          relatedBy: .Equal,
	                          toItem: v.superview!, attribute: .Top,
	                          multiplier: 1.0, constant: 0)

let leading = NSLayoutConstraint( item: v, attribute: .Leading,
	                              relatedBy: .Equal,
	                              toItem: v.superview!, attribute: .Leading,
	                              multiplier: 1.0, constant: 0)

let trailing = NSLayoutConstraint( item: v, attribute: .Trailing,
	                               relatedBy: .Equal,
	                               toItem: v.superview!, attribute: .Trailing,
	                               multiplier: 1.0, constant: 0)

let bottom = NSLayoutConstraint( item: v, attribute: .Bottom,
	                             relatedBy: .Equal,
	                             toItem: v.superview!, attribute: .Bottom,
	                             multiplier: 1.0, constant: 0)

v.superview?.addConstraints([top, leading, trailing, bottom])
Using KVConstraintKit

v +== [.Top, .Leading, .Trailing, .Bottom]


##### Similarly for margin constraints
> ```swift
v +== [ .LeadingMargin, .TrailingMargin, .TopMargin, .BottomMargin]

Fit

Horizontally

	view.applyConstraintFitHorizontallyToSuperview()
OR
	view.applyConstraintFitHorizontallyToSuperview(20) // padding

Vertically

	view.applyConstraintFitVerticallyToSuperview()
OR
	view.applyConstraintFitVerticallyToSuperview(20) // padding

Horizontally & Vertically

	view.applyConstraintFitToSuperview()
OR
	view.applyConstraintFitToSuperview(20) // width same padding for all edge

Fit with inset

	let inset = UIEdgeInsets(top: 4, left: 8, bottom: 12, right: 16)    
	view.applyConstraintFitToSuperview(contentInset:inset)

Size

Width

	view.applyWidthConstraint(100)
OR
	view.applyAtLeastWidthConstraint(100)
OR
	view.applyAtMostWidthConstraint(100)

Height

	view.applyHeightConstraint(100)
OR
	view.applyAtLeastHeightConstraint(100)
OR
	view.applyAtMostHeightConstraint(100)

Aspact Ratio

	view.applyAspectRatioConstraint()

Auto Layout Attributes

KVConstraintKit supports all built-in layout attributes as of iOS, tvOS & OSX, see the NSLayoutAttribute enum.

Quick Reference

Layout Attributes Using Operator Using Method
Leading (subview +== .Leading).constant = 20 subview.applyLeadingPinConstraintToSuperview(20)
Trailing (subview +== .Trailing).constant = 20 subview.applyTrailingPinConstraintToSuperview(20)
Top (subview +== .Top).constant = 20 subview.applyTopPinConstraintToSuperview(20)
Bottom (subview +== .Bottom).constant = 20 subview.applyBottomPinConstraintToSuperview(20)
CenterX subview +== .CenterX subview.applyCenterXPinConstraintToSuperview()
CenterY subview +== .CenterY subview.applyCenterYPinConstraintToSuperview()
Height subview +== (.Height, 100) subview.applyHeightConstraint(100)
Width subview +== (. Width, 100) subview.applyWidthConstraint(100)
CenterX & CenterY subview +== [.CenterX, .CenterY] subview.applyConstraintToCenterInSuperview()

For more details see the ApplyViewConstraint extension and LayoutRelationable Protocol of KVConstraintKit.

Note: Avoid using Left and Right attributes. Use Leading and Trailing instead. This allows the layout to adapt to the view’s reading direction. By default the reading direction is determined based on the current language set by the user.

License

KVConstraintKit is available under the MIT license. See the LICENSE file for more info.

Contributions

Any contribution is more than welcome! You can contribute through pull requests and issues on GitHub.

Author

If you wish to contact me, email at: keshavvbe@gmail.com

About

Impressive Auto Layout DSL for iOS, tvOS & OSX. & It is written in pure swift.

Resources

Stars

0 stars

Watchers

1 watching

Forks

Releases

Packages

Contributors

Languages