Latest 1.0.2
Homepage https://github.com/ustwo/autolayout-helper-swift
License MIT
Platforms ios 8.3, requires ARC
Authors ,

License
Build Status
codecov.io

AutoLayoutHelper

UIView extension to easily create common Auto Layout Constraints for iOS.

The Problem

Using Auto Layout programatically (before iOS 9’s Auto Layout API) can either be quite verbose i.e. building NSLayoutConstraint objects for each rule or error prone e.g. (using Visual Format Language strings)

A Solution

We can make creating common NSLayoutConstraint relations into some reusable methods we can call on any class that subclasses UIView. We also ensure the constraint created gets added to the view’s superview for you.

This means you can relate a view to another view with the NSLayoutAttribute or NSLayoutRelation you need, as part of the view’s setup and also helps us keep the code DRY.

Dependencies

Requirements

  • iOS 7+
  • tvOS 9+

Installation

CocoaPods

CocoaPods is a dependency manager for Cocoa projects. You can install it with the following command:

$ gem install cocoapods

To integrate AutoLayoutHelperSwift into your Xcode project using CocoaPods, specify it in your Podfile:

platform :ios, '8.3'

use_frameworks!

pod 'AutoLayoutHelperSwift', '~> 1.0.0'

Then, run the following command:

$ pod install

Manually

  • Add the UIView+AutoLayoutHelper.swift file to your Xcode project.
  • Note. Dynamic Framework target coming soon!

Usage

Examples:

Fill Superview

Add NSLayoutConstraint relations for a UIView relating its top, leading, trailing and bottom edges to it’s superview

// Create view

let leftView = UIView()
leftView.backgroundColor = UIColor.red
view.addSubview(leftView)

// Add constraints

leftView.addTopConstraint(toView: superview, attribute: .top, relation: .equal, constant: 10.0)
leftView.addLeadingConstraint(toView: superview, attribute: .leading, relation: .equal, constant: 10.0)
leftView.addTrailingConstraint(toView: superview, attribute: .trailing, relation: .equal, constant: -10.0)
leftView.addBottomConstraint(toView: superview, attribute: .bottom, relation: .equal, constant: -10.0)

or shorter you can omit the attributes:

leftView.addTopConstraint(toView: superview, constant: 10.0)
leftView.addLeadingConstraint(toView: superview, constant: 10.0)
leftView.addTrailingConstraint(toView: superview, constant: -10.0)
leftView.addBottomConstraint(toView: superview, constant: -10.0)

or even shorter using fillSuperView (via @danieladias )

let edgeInsets = UIEdgeInsets(top: 10.0, left: 10.0, bottom: 10.0, right: 10.0)
leftView.fillSuperView(edgeInsets)

Centering

Add constraints to center a UIView within its superview both vertically (Y axis) and horizontally (X axis):

label.addCenterXConstraintToView(label.superview, relation: .equal, constant:0.0)
label.addCenterYConstraintToView(label.superview, relation: .equal, constant:0.0)

Add constraints for a fixed width and height amount:

view.addWidthConstraintWithRelation: .equal, constant:100.0)
view.addHeightConstraintWithRelation: .equal, constant:80.0)

Modify constraints (since the methods return them to you)

// Create a reference to the `NSLayoutConstraint` e.g. for height

heightConstraint = view.addHeightConstraint(toView: nil, constant: 80.0)

...

// Update the height constant

heightConstraint.constant = 30.0;

// Animate changes

UIView.animateWithDuration(0.3, animations: { () -> Void in

    view.layoutIfNeeded()
})

Contributing

Please note that this project is released with a Contributor Code of Conduct. By participating in this project you agree to abide by its terms. See the Code of Conduct file

Maintainers

Contact

[email protected]

License

AutoLayoutHelper is released under the MIT License. See License.

Latest podspec

{
    "name": "AutoLayoutHelperSwift",
    "version": "1.0.2",
    "license": "MIT",
    "summary": "UIView extension to easily create common Auto Layout Constraints for iOS.",
    "homepage": "https://github.com/ustwo/autolayout-helper-swift",
    "authors": {
        "Shagun Madhikarmi": "[email protected]",
        "Daniela Dias": "[email protected]"
    },
    "source": {
        "git": "https://github.com/ustwo/autolayout-helper-swift.git",
        "tag": "v1.0.2"
    },
    "platforms": {
        "ios": "8.3"
    },
    "source_files": "Sources/*.swift",
    "requires_arc": true,
    "pushed_with_swift_version": "3.0"
}

Pin It on Pinterest

Share This