Homemade automatic layout constraint open source library Lite, Auto, Layout

First, attach the source address of this library: https://github.com/MellongLau/LiteAutoLayout

This library is written for a long time, because of the recent busy, have no time to update it up, especially the document, so long it is a point of completion. As for the good, then I Different people, different views., before more accustomed to using the IB drag auto layout constraints, this library is realized by using the concept of IB, the following are mentioned this point, so, personally think that the habit of using IB to the layout of the students should be relatively easy to use and used.

And then, if –

Like the project? Click on the Star button inside Github to support us!

Sample project

You can download the sample project running under the Demo folder, and here is a screenshot of the running.

Homemade automatic layout constraint open source library Lite, Auto, Layout

This is a login box interface that uses LiteAutoLayout to write layout constraints, and all of the constraint code is in the screenshot below:

Homemade automatic layout constraint open source library Lite, Auto, Layout

brief introduction

Recently in Swift 3, Swift 3 brings us a lot of new features, found in the learning process of this approach is very suitable for custom operators to write AutoLayout constraints, combined with their usual use IB to create the view of constraints between experience, decided to use a custom operator to make use of the code to create a constraint is more intuitive and clear and simple.

Among them, ~> the symbol was used as a constraint on my view or view its sibling parent view expressions, such as (view1 ~> view2), intermediate need to stay in space, and create for their own constraints, you can use (~> view1) to create the middle, don’t need to save space, add constraint is used a simple chain of writing.

Of course, if you don’t want to use ~&gt, this symbol creates constraints, I also provide startLayout () methods to create constraints directly, such as:

View1.startLayout (toView:, view2).

In addition, because of the use of IB interface more projects, but also think IB add constraints to the use of relatively simple and convenient, so here is set using a constraint method is to use the concept of IB adding constraints, I believe that useful IB should be relatively easy to accept this kind of writing, in the use of LiteAutoLayout that will deepen add layout constraints the understanding of IB, using two kinds of mutual promotion and mutual improvement, which is one of the advantages of the library.

Compare NSLayoutConstraint, vs, NSLayoutAnchor, vs, LiteAutoLayout

If you set the vertical distance between two views, constraint verticalSpacing (), it is worth noting that ~> the default view is above; if it is to add horizontal distance, then ~> the back view is on the left.

This library is a simple package, and you should be intimidated by its cumbersome wording if you have used SDK’s original method of creating constraints.

Here’s an example of Apple’s official website:

The use of NSLayoutConstraint NSLayoutConstraint / / create constraints (item: subview, attribute:.Leading, relatedBy:.Equal, toItem: view, attribute:.LeadingMargin, multiplier: 1, constant: 0).Active = true NSLayoutConstraint (item: subview, attribute:.Trailing, relatedBy:.Equal, toItem: view, attribute:.TrailingMargin, multiplier: 1, constant: 0).Active = true / Anchors using Layout to create the same let margins = view.layoutMarginsGuide subview.leadingAnchor.constraintEqualToAnchor constraints (margins.leadingAncho R) =.Active = true, subview.trailingAnchor.constraintEqualToAnchor (margins.trailingAnchor),.Active = true

You can see the direct use of NSLayoutConstraint to create a simple layout constraints need complicated code, the use of NSLayoutAnchor can simplify the code constraints, how the LiteAutoLayout in the end, we create the layout constraints of the same LiteAutoLayout:

The use of Lite Auto Layout / / create the same constraints (subview ~> view (.Leading)) (.Trailing)

Obviously, using LiteAutoLayout is even simpler than NSLayoutAnchor, and code reading is much easier to understand, and NSLayoutAnchor currently supports only iOS9+, and LiteAutoLayout currently supports iOS7+.

Finally, it is important to note that..:

  • On the left is the view, right view of the father (subview ~> view, subview view is on the left side of the view).
  • The left is the right or bottom of the view on the right (view2, ~&gt, view1, and view2 is on the right or lower of view1).

Current supported features:

  1. > < =, =, = =.
  2. Support specified priority.
  3. Support chaining.

How to install

Download the file LiteAutoLayout.swift and copy it to the project folder, adding references to the project.

Using LiteAutoLayout?

If you use LiteAutoLayout in your project, please send an email (mail address below) and let me know, and I’ll add your app download link here!


Welcome to this project to provide any help, including suggestions, funds, code or spiritual encouragement for this project, please submit the pull code with request code, after examination immediately merged into the main branch.


MIT License