Latest 1.0.0
Homepage https://github.com/steve-cuzzort/ReadableConstraints
License MIT
Platforms ios 8.0, requires ARC
Authors

ReadableConstraints lets you turn verbose layout constraint code into something more readable.

control1.rightAnchor.constraintEqualToAnchor(control2.leftAnchor, constant: 20).active = true

becomes

control2.isRightOf(control1, .By, 20)

You can also string together commands to make a readable layout in code.

button1.leftEdgeIsAlignedWith(baseView, .Plus, 10)
               .topEdgeIsAlignedWith(.Plus, 10)
               .isAsWideAs(.Minus, 20)

But thats not all! You can also specify if the constraint only applies in Portrait or Landscape!

button2.isBelow(button1.By, 10)
             .isAsWideAs(.ByRatio, 1.00, onlyFor: [.Portrait])
             .isAsWideAs(.ByRatio, 0.40, onlyFor: [.Landscape])
             .isVerticallyCenteredWith(.Exactly)

Or if the constraint should only be applied on a certain device

button2.isBelow(button1.By, 10)
             .isVerticallyCenteredWith(.EXACTLY, onlyFor:[.Ipad])

You can also specify another anchor point besides the default

button2.isBelow(button1.By, 10)
             .leftEdgeIsAlignedWith(.EXACTLY, fromThe: .Center)  
             .isVerticallyCenteredWith(.Exactly)

You can turn on the portrait or landscape in each view. ReadableConstraints will automatically turn on or off any orientation specific constraints in sub-views for you.

// somewhere in the code where you detect portait vs landscape
if(InPortraitMode())
{
  self.view.enablePortraitConstraints()
}
else
{
  self.view.enableLandscapeConstraints()
}

If you want ReadableConstraints to handle all the annoyances of handling re-layout on the screen rotation, then make these 2 changes in you AppDelegate

Add this call to applicationDidFinishLaunchingWithOptions

NSNotificationCenter.defaultCenter().addObserver(self, 
                                                selector: "screenRotated", 
                                                name: UIDeviceOrientationDidChangeNotification, 
                                                object: nil)

and add this function somewhere in AppDelegate

   func screenRotated()
   {
      switch(UIApplication.sharedApplication().statusBarOrientation)
      {
         case .Unknown: //Default orientation (which is portrait)
            fallthrough
         case .Portrait:
            fallthrough
         case .PortraitUpsideDown:
            self.window?.enablePortraitConstraints()
            self.window?.layoutSubviews()
            break;
         case .LandscapeLeft:
            fallthrough
         case .LandscapeRight:
            self.window?.enableLandscapeConstraints()
            self.window?.layoutSubviews()
      }
   }

Requirements

ReadableConstraints only runs on iOS 9+ devices.

Installation

ReadableConstraints is available through CocoaPods. To install it, add the following to your podfile:

pod "ReadableConstraints"

Or, simply copy the ReadableConstraint.swift file into your project.

Usage

The API consist of 12 commands to create constrants, 3 functions to help you manage your controls, and 3 properties, that let you get at the constraints on a per control basis. All of the below function are added to UIView.

Call Type Returns Default Anchor
isRightOf Function Self .Right
isLeftOf Function Self .Left
isHorizontallyCenteredWith Function Self .Center
isAbove Function Self .Top
isBelow Function Self .Bottom
isVerticallyCenteredWith Function Self .Center
rightEdgeIsAlignedWith Function Self .Right
leftEdgeIsAlignedWith Function Self .Left
topEdgeIsAlignedWith Function Self .Top
bottomEdgeIsAlignedWidh Function Self .Bottom
isAsWideAs Function Self .IsWide
isAsTallAs Function Self .IsTall
asConstraint Function NSLayoutConstraint! N/A
enablePortraitConstraints Function void N/A
enableLandscapeConstraints Function void N/A
alwaysConstraints Property void N/A
landscapeConstraints Property void N/A
portraitConstraints Property void N/A

All the constraint creating api calls API except isAsWideAs and isAsTallAs have the same arugment list. The API is meant to be read like an english statement. Lets take a look at isRightOf and isAbove

lhs.isRightOf(rhs,.By,10, fromThe:.Center onlyFor: [.Landscape, .Ipad])
  .isAbove(.By,20, onlyFor: [.Landscape, .Ipad]

The first parameter, rhs, is any UIView. If the command is not the first in a chain (like isAbove) the first paramter can be omitted.

The second parameter .BY specifies the direction to move. .BY is special, if you use isLeftOf or isAbove it will flip the sign on the number, to make it more readable. If you don’t like this behavior, check out the table below for .PLUS and .MINUS

Command Description
.By Move the LHS control in the direction specified by the command. Flips sign of constant for isLeftOf and isAbove.
.Exactly Same as .By with a constant of 0. Forces the LHS to be flush with RHS
.Plus Always move to the right or down by the constant
.Minus Always flips the sign of the constant before applying
.ByAtLeast Same as .By, but uses the greater than or equal to constraint instead of equals to
.ByAtMost Same as .By, but uses the less than or equal to constraint instead of equals
.ByRatio Uses the multiplier instead of constant field, this is most useful for isWideAs and isTallAs
.RatioOfAtLeast Same as .ByRatio, but uses greater than or equal to constraint
.RatioOfAtMost Same as .ByRatio, but uses less than or equal to constraint

fromThe: specifies an anchor point. Every command has a default anchor point.

Anchor Description
.left leading anchor
.Right trailing anchor
.Center center x anchor OR center y anchor depending on call
.Top top anchor
.Bottom bottom anchor
.IsWide width size anchor
.IsTall height size anchor

forOnly speicifies when the constraint should be used

Option Description
.Ipad Only use on iPad devices
.Iphone Only use on iPhone devices
.Landscape Only use in landscape orientation
.Portrait Only use in portrait orientation
.Always Always use (default value)

Author

Steve Cuzzort, [email protected]

License

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

Latest podspec

{
    "name": "ReadableConstraints",
    "version": "1.0.0",
    "summary": "A swift 2.0 library that makes it easier to write constraints using iOS9+ constraint system",
    "description": "ReadableConstraints aims to be a less cryptic, easy-to-read constraint systemnthat wraps the iOS9 constraint calls.  Instead of having to dedicate an entirenline to a constraint, simply write `ctr1.isLeftOf(ctr1.BY, 10).isAbove(.BY,10)`",
    "homepage": "https://github.com/steve-cuzzort/ReadableConstraints",
    "license": "MIT",
    "authors": {
        "Steve Cuzzort": "[email protected]"
    },
    "source": {
        "git": "https://github.com/steve-cuzzort/ReadableConstraints.git",
        "tag": "1.0.0"
    },
    "platforms": {
        "ios": "8.0"
    },
    "requires_arc": true,
    "source_files": "Pod/Classes/**/*",
    "resource_bundles": {
        "ReadableConstraints": [
            "Pod/Assets/*.png"
        ]
    }
}

Pin It on Pinterest

Share This