Swift Automatic Layout Framework-SnapKit, swift framework-snapkit

Source: Internet
Author: User
Tags install cocoapods

Swift Automatic Layout Framework-SnapKit, swift framework-snapkit

Official Website: http://snapkit.io/

Github: https://github.com/SnapKit/SnapKit

SnapKit is a DSL to make Auto Layout easy on both iOS and OS X.
  • Simple & Expressive chaining DSL allows building constraints with minimal amounts of code while ensuring they are easy to read and understand.
  • Type Safe by design to reduce programmer error and keep invalid constraints from being created in the first place for maximized productivity.
  • Compatible for both iOS and OS X apps installable through Cocoapods or Carthage.
  • Free to use in all projects and licensed under the flexible MIT license.
Requirements
  • IOS 7.0 +/OS X 10.9 +
  • Quick 2.0
  • Xcode 7.0 +

While SnapKit supports iOS 7.0 and OS X 10.9 these are considered legacy platforms, so you must manually integrate the source files directly. Please see the Legacy Platforms page for more information and steps.

Installing

The first thing you'll need to do with SnapKit is get it installed into your project. We support three different integrations:

Cocoapods

CocoaPods is a dependency manager for Cocoa projects.

CocoaPods 0.36 adds supports for Swift and embedded frameworks. You can install it with the following command:

$ gem install cocoapods

To integrate SnapKit into your Xcode project using CocoaPods, specify it in yourPodfile:

source 'https://github.com/CocoaPods/Specs.git'platform :ios, '8.0'use_frameworks!pod 'SnapKit', '~> 0.15.0'

Then, run the following command:

$ pod install
Carthage

Carthage is a decentralized dependency manager that automates the process of adding frameworks to your Cocoa application.

You can install Carthage with Homebrew using the following command:

$ brew update$ brew install carthage

To integrate SnapKit into your Xcode project using Carthage, specify it in yourCartfile:

github "SnapKit/SnapKit" >= 0.15.0
Embedded Framework
  • Add SnapKit as a submodule by opening the Terminal,cd-Ing into your top-level project directory, and entering the following command:
$ git submodule add https://github.com/SnapKit/SnapKit.git
  • OpenSnapKitFolder, and dragSnapKit.xcodeprojInto the file navigator of your app project.
  • In Xcode, navigate to the target configuration window by clicking on the blue project icon, and selecting the application target under the "Targets" heading in the sidebar.
  • Ensure that the deployment targetSnapKit.frameworkMatches that of the application target.
  • In the tab bar at the top of that window, open the "Build Phases" panel.
  • Expand the "Target Dependencies" group, and addSnapKit.framework.
  • Click on+Button at the top left of the panel and select "New Copy Files Phase". Rename this new phase to "Copy Frameworks", set the "Destination" to "Frameworks", and addSnapKit.framework.
Usage

SnapKit is designed to be extremely easy to use. Let's say we want to layout a box that is constrained to it's superview's edges with 20pts of padding.

let box = UIView()superview.addSubview(box)box.snp_makeConstraints { (make) -> Void in    make.top.equalTo(superview).offset(20)    make.left.equalTo(superview).offset(20)    make.bottom.equalTo(superview).offset(-20)    make.right.equalTo(superview).offset(-20)}

Or even shorter:

let box = UIView()superview.addSubview(box)box.snp_makeConstraints { (make) -> Void in    make.edges.equalTo(superview).inset(UIEdgeInsetsMake(20, 20, 20, 20))}

Not only does this greatly shorten and increase the readability of constraints SnapKit is also taking care of a few crucial steps in the process:

  • Determining the best common superview to install the constraints on.
  • Keeping track of the constrainted installed so they can easily be removed later.
  • EnsuringsetTranslatesAutoresizingMaskIntoConstraints(false)Is called on all appropriate views.
Not all things are created equal

.equalTo equivalent to NSLayoutRelation.Equal

.lessThanOrEqualTo equivalent to NSLayoutRelation.LessThanOrEqual

.greaterThanOrEqualTo equivalent to NSLayoutRelation.GreaterThanOrEqual

These three consistency ity constraints accept one argument which can be any of the following:

1. ViewAttribute
make.centerX.lessThanOrEqualTo(view2.snp_left) 
make.centerX.lessThanOrEqualTo(view2.snp_left)

ViewAttribute NSLayoutAttribute
View. snp_left NSLayoutAttribute. Left
View. snp_right NSLayoutAttribute. Right
View. snp_top NSLayoutAttribute. Top
View. snp_bottom NSLayoutAttribute. Bottom
View. snp_leading NSLayoutAttribute. Leading
View. snp_trailing NSLayoutAttribute. Trailing
View. snp_width NSLayoutAttribute. Width
View. snp_height NSLayoutAttribute. Height
View. snp_centerX NSLayoutAttribute. CenterX
View. snp_centerY NSLayoutAttribute. CenterY
View. snp_baseline NSLayoutAttribute. Baseline


If you want view. left to be greater than or equal to label. left: 2. UIView/NSView
// these two constraints are exactly the samemake.left.greaterThanOrEqualTo(label)make.left.greaterThanOrEqualTo(label.snp_left)

  

3. Strict Checks

Auto Layout allows width and height to be set to constant values. if you want to set view to have a minimum and maximum width you cocould pass a primitive to the fill ity blocks:

// width >= 200 && width <= 400make.width.greaterThanOrEqualTo(200)make.width.lessThanOrEqualTo(400)

  

However Auto Layout does not allow alignment attributes such as left, right, centerY etc to be set to constant values. so if you pass a primitive for these attributes SnapKit will turn these into constraints relative to the view's superview ie:

// creates view.left <= view.superview.left + 10make.left.lessThanOrEqualTo(10)

  

You can also use other primitives and structs to build your constraints, like so:

make.top.equalTo(42)make.height.equalTo(20)make.size.equalTo(CGSizeMake(50, 100))make.edges.equalTo(UIEdgeInsetsMake(10, 0, 10, 0))make.left.equalTo(view).offset(UIEdgeInsetsMake(10, 0, 10, 0))

  

Learn to prioritize

.prority allows you to specify an exact priority

.priorityHigh equivalent to UILayoutPriority.DefaultHigh

.priorityMedium is half way between high and low

.priorityLow equivalent to UILayoutPriority.DefaultLow

Priorities are can be tacked on to the end of a constraint chain like so:

make.left.greaterThanOrEqualTo(label.snp_left).priorityLow()make.top.equalTo(label.snp_top).priority(600)

  

Composition, composition, composition

SnapKit also gives you a few convenience methods to create multiple constraints at the same time.

Edges
// make top, left, bottom, right equal view2make.edges.equalTo(view2);// make top = superview.top + 5, left = superview.left + 10,//      bottom = superview.bottom - 15, right = superview.right - 20make.edges.equalTo(superview).inset(UIEdgeInsetsMake(5, 10, 15, 20))

  

Size
// make width and height greater than or equal to titleLabelmake.size.greaterThanOrEqualTo(titleLabel)// make width = superview.width + 100, height = superview.height - 50make.size.equalTo(superview).offset(CGSizeMake(100, -50))

  

Center
// make centerX and centerY = button1make.center.equalTo(button1)// make centerX = superview.centerX - 5, centerY = superview.centerY + 10make.center.equalTo(superview).offset(CGPointMake(-5, 10))

  

You can chain view attributes for increased readability:

// All edges but the top should equal those of the superviewmake.left.right.bottom.equalTo(superview)make.top.equalTo(otherView)

  

Hold on for dear life

Sometimes you need modify existing constraints in order to animate or remove/replace constraints. In SnapKit there are a few different approaches to updating constraints.

1. References

You can hold on to a reference of a participating constraint by assigning the result of a constraint make expression to a local variable or a class property. you cocould also reference multiple constraints by storing them away in an array.

var topConstraint: Constraint? = nil...// when making constraintsview1.snp_makeConstraints { (make) -> Void in  self.topConstraint = make.top.equalTo(superview).offset(padding.top).constraint  make.left.equalTo(superview).offset(padding.left)}...// then later you can callself.topConstraint.uninstall()// or if you want to update the constraintself.topConstraint.updateOffset(5)

  

2. snp_updateConstraints

Alternative if you are only updating the constant value of the constraint you can use the methodsnp_updateConstraintsInsteadsnp_makeConstraints

// this is Apple's recommended place for adding/updating constraints// this method can get called multiple times in response to setNeedsUpdateConstraints// which can be called by UIKit internally or in your code if you need to trigger an update to your constraintsoverride func updateConstraints() {    self.growingButton.snp_updateConstraints { (make) -> Void in        make.center.equalTo(self);        make.width.equalTo(self.buttonSize.width).priorityLow()        make.height.equalTo(self.buttonSize.height).priorityLow()        make.width.lessThanOrEqualTo(self)        make.height.lessThanOrEqualTo(self)    }    // according to apple super should be called at end of method     super.updateConstraints()}

  

3. snp_remakeConstraints

snp_remakeConstraintsIs similarsnp_makeConstraints, But will first remove all existing constraints installed by SnapKit.

func changeButtonPosition() {  self.button.snp_remakeConstraints { (make) -> Void in     make.size.equalTo(self.buttonSize)    if topLeft {      make.top.left.equalTo(10)    } else {      make.bottom.equalTo(self.view).offset(-10)      make.right.equalTo(self.view).offset(-10)    }  }}

  

Features
  • Not limited to a subset of Auto Layout. Anything NSLayoutConstraint can do SnapKit can also do.
  • Better debugging support to help find breaking constraints.
  • No crazy operator overloads.
  • Not string or dictionary based and you get the strictest compile time checks possible.
TODO
  • Example Projects
  • Better Debugging Support

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.