Giter Club home page Giter Club logo

swiftlayoutconstraints's Introduction

SwiftLayoutConstraints

CI Status Carthage compatible Version License Platform

SwiftLayoutConstraints aims to provide expressive NSLayoutConstraint with Swift.

Compatibility

SwiftLayoutConstraints targets Swift 2.2 and iOS 8

Constraints

Basic Constraints can be made such as:

// assume we have 2 views and they are added to one parent (view)
let view1 = UIView()
view1.translatesAutoresizingMaskIntoConstraints = false
view1.backgroundColor = UIColor.greenColor()
let view2 = UIView()
view2.translatesAutoresizingMaskIntoConstraints = false
view2.backgroundColor = UIColor.blueColor()
view.addSubview(view1)
view.addSubview(view2)

// Expressions below create, **activate** and return constraints
view1.ls_top | .Left | .Right | view2.ls_left | .Right | .Bottom ~== view
view2.ls_top ~== view1.ls_bottom + 40
view1.ls_height ~== view.ls_height * 0.25 + 50

The above code generates this output:
Example

Overview

LayoutConstraints are represented as LayoutConstraintType, and has two implementations:

  1. LhsLayoutConstraint represents left-hand side, and does NOT support mutating its constant and multiplier.
  2. RhsLayoutConstraint represents right-hand side, and supports mutating its constant and multiplier.

Relation Operators

SwiftLayoutConstraints introduces operators to represent each NSLayoutRelation, simply ~ is added before the expressive form of the relation.

  • ~== for NSLayoutRelation.Equal
  • ~>= for NSLayoutRelation.GreaterThanOrEqual
  • ~<= for NSLayoutRelation.LessThanOrEqual

Supported Expressions

Representing any Relation Operator as :op, the following expressions are supported:

LhsLayoutConstraint :op LayoutConstraintType
LhsLayoutConstraint :op CGFloat
[LhsLayoutConstraint] :op AnyObject
AnyObject :op [LayoutConstraintType]
[LhsLayoutConstraint] :op [LayoutConstraintType]

Quick Notes

  1. At any point, Left-hand side of any expression is always LhsLayoutConstraint
  2. You may create an array of constraints using | operator, i.e: view.ls_height | view.ls_width
  3. Operator | also accepts NSLayoutAttribute where values (object, constant and multiplier) are taken from previous constraint, i.e: view.ls_top | .Bottom
  4. You may use + or * to set constraint's constant and multiplier, i.e: view1.ls_top ~== view2.ls_bottom * 1 + 20
  5. Consider operators precedence in your expressions, i.e: view.ls_top * 0.5 + 50 is not same as view.ls_top + 50 * 0.5

Installation

github "ahmadbaraka/SwiftLayoutConstraints" ~> 1.0
pod 'SwiftLayoutConstraints', '~> 1.0'

License

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

swiftlayoutconstraints's People

Contributors

ahmadbaraka avatar

Watchers

James Cloos avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.