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

Overview

KVConstraintKit

CI Status Version Carthage compatible Swift 3.x Swift 4.x License Platform

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

KVConstraintKit

Installation

Using CocoaPods

  1. To integrate KVConstraintKit into your Xcode project using CocoaPods, specify it to a target in your Podfile:
use_frameworks!
pod 'KVConstraintKit', '~> 2.1'

If you want to be on the bleeding edge, replace the last line with:

pod 'KVConstraintKit', :git => 'https://github.com/keshavvishwkarma/KVConstraintKit.git', :branch => 'master'
  1. Run pod install and open the open the {Project}.xcworkspace instead of the {Project}.xcodeproj file to launch Xcode.

Using Carthage

  1. To integrate KVConstraintKit into your Xcode project using Carthage, specify it in your Cartfile:
github "keshavvishwkarma/KVConstraintKit" ~> 2.1
  1. Run carthage update and follow the additional steps in order to add KVConstraintKit to your project.

Auto Layout Attributes

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

Usage

Here's a quick example:

Without Using KVConstraintKit
let v = UIView.prepareAutoLayoutView()
v.backgroundColor = UIColor.red
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
v +== [ .leadingMargin, .trailingMargin, .topMargin, .bottomMargin]

Fit

Horizontally

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

Vertically

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

Horizontally & Vertically

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

Fit with inset

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

Center

Horizontally

view.applyCenterX()
OR
view.applyCenterX(20) // X offset

Vertically

view.applyCenterY()
OR
view.applyCenterY(20) // Y offset

Horizontally & Vertically

view.applyCenter()
OR
view.applyCenter(CGPoint(x:20, y:20)) // XY offset
OR
view.applyCenterX(4).applyCenterY(16) // XY offset

Size

Width

view.applyWidth(100)
OR
view.applyAtLeastWidth(100)
OR
view.applyAtMostWidth(100)

Height

view.applyHeight(100)
OR
view.applyAtLeastHeight(100)
OR
view.applyAtMostHeight(100)

Aspact Ratio

view.applyAspectRatio()

Quick Reference

Layout Attributes Using Operator Using Method
Leading (subview +== .leading).constant = 20 subview.applyLeading(20)
Trailing (subview +== .trailing).constant = 20 subview.applyTrailing(20)
Top (subview +== .top).constant = 20 subview.applyTop(20)
Bottom (subview +== .bottom).constant = 20 subview.applyBottom(20)
CenterX subview +== .centerX subview.applyCenterX()
CenterY subview +== .centerY subview.applyCenterY()
Height subview +== (.height, 100) subview.applyHeight(100)
Width subview +== (.width, 100) subview.applyWidth(100)
CenterX & CenterY subview +== [.centerX, .centerY] subview.applyCenter()

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.

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 constraint with multiplier
*>= to add greater than or equal relation constraint with multiplier
*<= to add less than or equal relation constraint with multiplier
|==| to add or equal relation constraint between sibling views
|>=| to add greater than or equal relation constraint between sibling views
|<=| to add less than or equal relation constraint between sibling views

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: [email protected]

You might also like...
 BrickKit is a delightful layout library for iOS and tvOS. It is written entirely in Swift!
BrickKit is a delightful layout library for iOS and tvOS. It is written entirely in Swift!

BrickKit is a delightful layout library for iOS and tvOS. It is written entirely in Swift! Deprecated BrickKit is being phased out at Wayfair, and the

Random-Colors-iOS - Random colors generator app with auto layout
Random-Colors-iOS - Random colors generator app with auto layout

Random Colors Random colors generator app with auto layout Demo demo.mp4 Depende

Lightweight Swift framework for Apple's Auto-Layout
Lightweight Swift framework for Apple's Auto-Layout

I am glad to share with you a lightweight Swift framework for Apple's Auto-Layout. It helps you write readable and compact UI code using simple API. A

Auto Layout In Swift Made Easy

Swiftstraints Swiftstraints can turn verbose auto-layout code: let constraint = NSLayoutConstraint(item: blueView, attr

Lightweight declarative auto-layout framework for Swift

SwiftyLayout SwiftyLayout is a framework that allows to describe layout constraints (ie NSLayoutConstraint) as a simple mathematical formula in a Swif

Minimal Auto Layout in Swift
Minimal Auto Layout in Swift

Restraint Restraint is a very very small library to help make your use of NSLayoutConstraint in Swift more legible & declarative. Like programmatic vi

Swift microframework for declaring Auto Layout constraints functionally
Swift microframework for declaring Auto Layout constraints functionally

Relayout Relayout is a Swift microframework to make using Auto Layout easier with static and dynamic layouts. Why? If you want to build a UI using App

SuperLayout is a Swift library that makes using Auto Layout a breeze.
SuperLayout is a Swift library that makes using Auto Layout a breeze.

SuperLayout is a library that adds a few custom operators to Swift that makes using the amazing NSLayoutAnchor API for Auto Layout a breeze. SuperLayo

Declarative Auto Layout in Swift, clean and simple

Tails Tails is a take on declarative Auto Layout. If you don't like typing (like me), it might be your kind of thing! Tails is written in Swift and cu

Comments
  • Swift 4.2 support.

    Swift 4.2 support.

    I am using your library on Xcode 10.1 with cocoa pod. Previously I was using Xcode 9.4 and it was working fine but I am getting so many error after Xcode update. It comes when I have used command pod repo update and then pod install. I am using swift 4.2 so might be issue with swift version but im not sure sure.

    enhancement help wanted good first issue 
    opened by Jain-Siddharth 2
  • Unwanted warnings on depreciation

    Unwanted warnings on depreciation

    Seems like depression message causing so many warnigs can you fix this in your next update. Every syntex starting with @available having the warning which says.

    Unexpected version number in 'available' attribute for non-specific platform '*'

    opened by Jain-Siddharth 0
Releases(2.1)
Owner
Keshav Vishwkarma
Iโ€™m a passionate software developer, enthusiast, and lifelong learner
Keshav Vishwkarma
Swifty DSL for programmatic Auto Layout in iOS

WWLayout Easy to write auto layout constraints, with minimal extensions to standard namespaces. Feature Highlights Easy to use, readable API Backwards

WW Tech 49 Oct 2, 2022
A declarative Auto Layout DSL for Swift :iphone::triangular_ruler:

Cartography ?? ?? Using Cartography, you can set up your Auto Layout constraints in declarative code and without any stringly typing! In short, it all

Robb Bรถhnke 7.3k Jan 4, 2023
A compact but full-featured Auto Layout DSL for Swift

Mortar allows you to create Auto Layout constraints using concise, simple code statements. Use this: view1.m_right |=| view2.m_left - 12.0 Instead of:

Jason Fieldman 83 Jan 29, 2022
Yet Another Swift Auto Layout DSL

FormationLayout Documentation FormationLayout is the top level layout class for one root view. FormationLayout takes a UIView as its rootView. transla

Evan Liu 53 Mar 31, 2022
Harness the power of AutoLayout NSLayoutConstraints with a simplified, chainable and expressive syntax. Supports iOS and OSX Auto Layout

Masonry Masonry is still actively maintained, we are committed to fixing bugs and merging good quality PRs from the wider community. However if you're

null 18k Jan 5, 2023
Written in pure Swift, QuickLayout offers a simple and easy way to manage Auto Layout in code.

QuickLayout QuickLayout offers an additional way, to easily manage the Auto Layout using only code. You can harness the power of QuickLayout to align

Daniel Huri 243 Oct 28, 2022
Fast Swift Views layouting without auto layout. No magic, pure code, full control and blazing fast

Fast Swift Views layouting without auto layout. No magic, pure code, full control and blazing fast. Concise syntax, intuitive, readable & chainable. [iOS/macOS/tvOS/CALayer]

layoutBox 2.1k Jan 2, 2023
Auto Layout (and manual layout) in one line.

Auto Layout (and manual layout) in one line. Quick Look view.bb.centerX().below(view2).size(100) Itโ€™s equivalent to iOS 9 API: view.centerXAnchor.cons

Javier Zhang 74 Oct 19, 2022
Auto Layout made easy with the Custom Layout.

Auto Layout made easy with the Custom Layout. Getting started CocoaPods CocoaPods is a dependency manager for Cocoa projects. You can install it with

Malith Nadeeshan 1 Jan 16, 2022
The ultimate API for iOS & OS X Auto Layout โ€” impressively simple, immensely powerful. Objective-C and Swift compatible.

The ultimate API for iOS & OS X Auto Layout โ€” impressively simple, immensely powerful. PureLayout extends UIView/NSView, NSArray, and NSLayoutConstrai

PureLayout 7.6k Jan 6, 2023