All Projects → roberthein → Tinyconstraints

roberthein / Tinyconstraints

Licence: mit
Nothing but sugar.

Programming Languages

swift
15916 projects
objective c
16641 projects - #2 most used programming language
ruby
36898 projects - #4 most used programming language

Projects that are alternatives of or similar to Tinyconstraints

EZAnchor
An easier and faster way to code Autolayout
Stars: ✭ 25 (-99.33%)
Mutual labels:  layout, constraints, constraint, sugar, nslayoutconstraint, superview, center
Stevia
🍃 Concise Autolayout code
Stars: ✭ 3,182 (-14.49%)
Mutual labels:  layout, constraints, auto, nslayoutconstraints
SuperPuperDuperLayout
Super puper duper mega easy awesome wrapper over auto layout!!111!!1!!!1!!!11111!!!1!!
Stars: ✭ 14 (-99.62%)
Mutual labels:  layout, constraints, sugar, auto
Easypeasy
Auto Layout made easy
Stars: ✭ 1,877 (-49.56%)
Mutual labels:  layout, constraints, auto
Snapkit
A Swift Autolayout DSL for iOS & OS X
Stars: ✭ 18,091 (+386.19%)
Mutual labels:  layout, constraints, auto
Kvconstraintkit
An Impressive Auto Layout DSL for iOS, tvOS & OSX. & It is written in pure swift.
Stars: ✭ 91 (-97.55%)
Mutual labels:  layout, constraints, auto
Bouncylayout
Make. It. Bounce.
Stars: ✭ 4,035 (+8.44%)
Mutual labels:  library, libraries, layout
dynamic-motion
Provide additional functionality to Android MotionLayout.
Stars: ✭ 34 (-99.09%)
Mutual labels:  layout, constraint
VanillaConstraints
🍦 Simplified and chainable AutoLayout constraints for iOS.
Stars: ✭ 42 (-98.87%)
Mutual labels:  layout, constraints
Swift101
That contains various information and examples about the basics of Swift Programming. 💻 📱 📺 ⌚️
Stars: ✭ 28 (-99.25%)
Mutual labels:  swift5, swift-5
Extras
Just some extras..
Stars: ✭ 17 (-99.54%)
Mutual labels:  swift5, swift-5
Table
CLI tables in Swift
Stars: ✭ 53 (-98.58%)
Mutual labels:  swift5, swift-5
Keepwords
📱🔐 Need an iOS password managing app with no pods? We got you covered!
Stars: ✭ 17 (-99.54%)
Mutual labels:  swift5, swift-5
IBLayoutConstraint
📏 Configure constraints for each device in Interface Builder with IBLayoutConstraint.
Stars: ✭ 24 (-99.36%)
Mutual labels:  swift5, swift-5
C Macro Collections
Easy to use, header only, macro generated, generic and type-safe Data Structures in C
Stars: ✭ 192 (-94.84%)
Mutual labels:  stack, library
wwlayout
Swifty DSL for programmatic Auto Layout in iOS
Stars: ✭ 46 (-98.76%)
Mutual labels:  layout, constraints
Hippolyte
HTTP Stubbing in Swift
Stars: ✭ 109 (-97.07%)
Mutual labels:  swift5, swift-5
Flutter Layouts Exampls
Layout of the flutter example.such as Row,Comlun,listview,Just for learning.
Stars: ✭ 292 (-92.15%)
Mutual labels:  stack, layout
Easyswiftlayout
Lightweight Swift framework for Apple's Auto-Layout
Stars: ✭ 345 (-90.73%)
Mutual labels:  layout, constraints
Stack Up.js
Create fixed width, variable height grid layouts.
Stars: ✭ 117 (-96.86%)
Mutual labels:  stack, layout

TinyConstraints is the syntactic sugar that makes Auto Layout sweeter for human use.

TinyConstraints

Features

  • Pure Swift 5 sweetness.
  • Everything you can do with Auto Layout, but shorter.
  • Constraints are active by default.
  • 100% compatible with other Auto Layout code.
  • Optionally store your constraints.
  • Set constraint priorities upon creation.
  • Constrain directly to the superview.
  • Stack views together with one line of code.
  • No need to set translatesAutoresizingMaskIntoConstraints because TinyConstraints does it for you.

Examples

Edges

Attaching a view to its superview with NSLayoutConstraint:

NSLayoutConstraint.activate([
    view.topAnchor.constraint(equalTo: superview.topAnchor, constant: 0),
    view.leadingAnchor.constraint(equalTo: superview.leadingAnchor, constant: 0),
    view.bottomAnchor.constraint(equalTo: superview.bottomAnchor, constant: 0),
    view.trailingAnchor.constraint(equalTo: superview.trailingAnchor, constant: 0)
])

with TinyConstraints:

view.edgesToSuperview()

or:

view.edgesToSuperview(insets: .top(10) + .left(10))

Center

Constraining the center of a view to its superview with NSLayoutConstraint:

NSLayoutConstraint.activate([
    view.centerXAnchor.constraint(equalTo: superview.centerXAnchor, constant: 0)
    view.centerYAnchor.constraint(equalTo: superview.centerYAnchor, constant: 0)
])

with TinyConstraints:

view.center(in: superview)

or:

view.center(in: superview, offset: CGPoint(x: 10, y: 10))

Basic Use

Typealiases

TinyConstraints gives you convenient and tiny typealiases for handling constraints.

  • Constraint = NSLayoutConstraint
  • Constraints = [NSLayoutConstraint]

Equal and Unequal Anchors

This constraints the top-anchor of the view to the top-anchor of the superview:

view.top(to: superview)

This constraints the top-anchor of firstView to the bottom-anchor of secondView:

firstView.topToBottom(of: secondView)

Constrain to Superview

Often you need to constrain a view to it's superview, with TinyConstraints you can do this super easy:

view.edgesToSuperview()

Or only one edge:

view.topToSuperview()

Or you can attach all edges except one, like this:

view.edgesToSuperview(excluding: .bottom)

Relation and Priority

For almost all constraints you can set the relation and priority properties. The default relation is .equal and the default priority is .required:

container.width(150, relation: .equalOrLess, priority: .high)

Storing Constraints

Here we create a set of inactive constraints and store these to our property:

let constraints = view.size(CGSize(width: 100, height: 100), isActive: false)

Activation and Deactivation

Besides the default NSLayoutConstraint activation, TinyConstraints also provides a way to activate a set of constraints:

constraints.activate()

You can also do this in an animation:

oldConstraints.deActivate()

constraints.activate()
UIViewPropertyAnimator(duration: 1, dampingRatio: 0.4) {
    self.layoutIfNeeded()
}.startAnimation()

Animating Constraint Constants

Here we add a height constraint to a view, store it and animate it later:

let height = view.height(100)

height.constant = 200
UIViewPropertyAnimator(duration: 1, dampingRatio: 0.4) {
    self.layoutIfNeeded()
}.startAnimation()

Stack

Stack provides a way of constraining views together in a superview:

let views = [logo, title, description]
superview.stack(views, axis: .vertical, spacing: 10)
Find these examples and more in the Example Project.

Installation

CocoaPods

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

pod "TinyConstraints"

Carthage

TinyConstraints is available through Carthage. To install it, simply add the following line to your Cartfile:

github "roberthein/TinyConstraints"

Swift Package Manager

TinyConstraints is available through Swift Package Manager. To install it, in Xcode 11.0 or later select File > Swift Packages > Add Package Dependency... and add TinyConstraints repository URL:

https://github.com/roberthein/TinyConstraints.git

Tutorials

Here are some video tutorials made by Alex Nagy.

Suggestions or feedback?

Feel free to create a pull request, open an issue or find me on Twitter.

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].