ProductPromotion
Logo

Swift

made by https://0x3d.site

GitHub - Skyvive/Swiftstraints: Auto Layout In Swift Made Easy
Auto Layout In Swift Made Easy. Contribute to Skyvive/Swiftstraints development by creating an account on GitHub.
Visit Site

GitHub - Skyvive/Swiftstraints: Auto Layout In Swift Made Easy

GitHub - Skyvive/Swiftstraints: Auto Layout In Swift Made Easy

Swiftstraints

Swiftstraints can turn verbose auto-layout code:

let constraint = NSLayoutConstraint(item: blueView,
                               attribute: NSLayoutAttribute.Width,
                               relatedBy: NSLayoutRelation.Equal,
                                  toItem: redView,
                               attribute: NSLayoutAttribute.Width,
                              multiplier: 1.0,
                                constant: 0.0)

Into one just one line of code:

let constraint = blueView.widthAnchor == redView.widthAnchor

Or transform your less than consise visual format language code:

let constraints = NSLayoutConstraint.constraintsWithVisualFormat("H:|[leftView]-10-[rightView]|",
                               options: NSLayoutFormatOptions(0),
                               metrics: nil,
                               views: ["leftView":leftView, "rightView":rightView])

Into the following:

let constraints = NSLayoutConstraints("H:|[\(leftView)]-10-[\(rightView)]|")

That was easy!

Installation

Swiftstraints is available through CocoaPods. To install, simply include the following lines in your podfile:

use_frameworks!
pod 'Swiftstraints'

Be sure to import the module at the top of your .swift files:

import Swiftstraints

Alternatively, clone this repo or download it as a zip and include the classes in your project.

Constraints

With Swiftstraints you can create constraints that look just Apple's generic constraint definition:

item1.attribute1 = multiplier ร— item2.attribute2 + constant

Swifstraints utilizes the new layout anchors introduced in iOS 9:

let view = UIView()
view.widthAnchor
view.heightAnchor
view.trailingAnchor
view.centerXAnchor
etc...

Swiftstraints implements operator overloading so that you can easily create custom constraints:

let blueView = UIView()
let redView = UIView()
let constraint = blueView.heightAnchor == redView.heightAnchor

Just as you would expect, you can specify a multiplier:

let constraint = blueView.heightAnchor == 2.0 * redView.heightAnchor

Or add a constant:

let constraint = blueView.heightAnchor == redView.heightAnchor + 10.0

You can specify inequalities:

let constraint = blueView.heightAnchor <= redView.heightAnchor

And you can define constant constraints if you so choose:

let constraint = blueView.heightAnchor == 100.0

Swiftstraints can readily compute relatively complex constraints:

let constraint = blueView.heightAnchor * 1.4 - 5.0 >= redView.heightAnchor / 3.0 + 400

It's really easy.

Visual Format Language

Apple provides an API that lets you create multiple constraints simultaneously with the Visual Format Language. As we saw before it can be a little cumbersome:

let constraints = NSLayoutConstraint.constraintsWithVisualFormat("H:|[leftView]-10-[rightView]|",
                               options: NSLayoutFormatOptions(0),
                               metrics: nil,
                               views: ["leftView":leftView, "rightView":rightView])

Swiftstraints uses string interpolation to let you specify the same constraints in one line of code:

let constraints = NSLayoutConstraints("H:|[\(leftView)]-10-[\(rightView)]|")

Swiftstraints also extends UIView so that you can add constraints easily using the interpolated string format:

superview.addConstraints("H:|[\(leftView)]-10-[\(rightView)]|")

Super easy, super simple.

Revision History

  • 3.0.1 - Bug fixes and limited iOS 8 support (Thank you catjia1011)
  • 3.0.0 - Updated to Swift 3
  • 2.2.0 - Added support for UILayoutPriority
  • 2.1.0 - Fixed a view reference bug and added a new convenience method for adding constraints
  • 2.0.2 - Added support for tvOS target.
  • 2.0.1 - Updated to include support for axis anchors, increased test coverage and more documentation.
  • 2.0.0 - Updated for Swift 2.0 and iOS 9. Now uses layout anchors for simple constraints and string interpolation for Visual Format Language constraints.
  • 1.1.0 - Minor API tweaks
  • 1.0.0 - Initial Release

Author

Brad Hilton, [email protected]

License

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

More Resources
to explore the angular.

mail [email protected] to add your project or resources here ๐Ÿ”ฅ.

Related Articles
to learn about angular.

FAQ's
to learn more about Angular JS.

mail [email protected] to add more queries here ๐Ÿ”.

More Sites
to check out once you're finished browsing here.

0x3d
https://www.0x3d.site/
0x3d is designed for aggregating information.
NodeJS
https://nodejs.0x3d.site/
NodeJS Online Directory
Cross Platform
https://cross-platform.0x3d.site/
Cross Platform Online Directory
Open Source
https://open-source.0x3d.site/
Open Source Online Directory
Analytics
https://analytics.0x3d.site/
Analytics Online Directory
JavaScript
https://javascript.0x3d.site/
JavaScript Online Directory
GoLang
https://golang.0x3d.site/
GoLang Online Directory
Python
https://python.0x3d.site/
Python Online Directory
Swift
https://swift.0x3d.site/
Swift Online Directory
Rust
https://rust.0x3d.site/
Rust Online Directory
Scala
https://scala.0x3d.site/
Scala Online Directory
Ruby
https://ruby.0x3d.site/
Ruby Online Directory
Clojure
https://clojure.0x3d.site/
Clojure Online Directory
Elixir
https://elixir.0x3d.site/
Elixir Online Directory
Elm
https://elm.0x3d.site/
Elm Online Directory
Lua
https://lua.0x3d.site/
Lua Online Directory
C Programming
https://c-programming.0x3d.site/
C Programming Online Directory
C++ Programming
https://cpp-programming.0x3d.site/
C++ Programming Online Directory
R Programming
https://r-programming.0x3d.site/
R Programming Online Directory
Perl
https://perl.0x3d.site/
Perl Online Directory
Java
https://java.0x3d.site/
Java Online Directory
Kotlin
https://kotlin.0x3d.site/
Kotlin Online Directory
PHP
https://php.0x3d.site/
PHP Online Directory
React JS
https://react.0x3d.site/
React JS Online Directory
Angular
https://angular.0x3d.site/
Angular JS Online Directory