Forked from adajos/ConstraintsWithNSLayoutAnchors.swift
Created
May 10, 2016 13:59
-
-
Save abreu-dev/b1f39855fd7952116e89188d1c9d422d to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import UIKit | |
func addViewAndButton() { | |
// iOS 9 availability coolness | |
guard #available(iOS 9, *) else { | |
return | |
} | |
// boilerplate view creation | |
let containerView = UIView(frame: CGRectMake(0, 0, 200, 200)) | |
containerView.backgroundColor = UIColor.grayColor() | |
let stopButton = UIButton(type: UIButtonType.Custom) | |
stopButton.backgroundColor = UIColor.redColor() | |
stopButton.setTitle("Red", forState: .Normal) | |
stopButton.setTitleColor(UIColor.blackColor(), forState: .Normal) | |
let cautionButton = UIButton(type: .Custom) | |
cautionButton.backgroundColor = UIColor.yellowColor() | |
cautionButton.setTitle("Yellow", forState: .Normal) | |
cautionButton.setTitleColor(UIColor.blackColor(), forState: .Normal) | |
let goButton = UIButton(type: .Custom) | |
goButton.backgroundColor = UIColor.greenColor() | |
goButton.setTitle("Green", forState: .Normal) | |
goButton.setTitleColor(UIColor.blackColor(), forState: .Normal) | |
containerView.addSubview(stopButton) | |
containerView.addSubview(cautionButton) | |
containerView.addSubview(goButton) | |
// Here's how easy it is to create NSLayoutConstraints with NSLayoutAnchors. | |
stopButton.topAnchor.constraintEqualToAnchor(containerView.topAnchor, constant: 10).active = true | |
stopButton.leadingAnchor.constraintEqualToAnchor(containerView.leadingAnchor, constant: 20).active = true | |
stopButton.trailingAnchor.constraintEqualToAnchor(containerView.trailingAnchor, constant: -20).active = true | |
stopButton.heightAnchor.constraintEqualToConstant(30).active = true | |
cautionButton.leadingAnchor.constraintEqualToAnchor(stopButton.leadingAnchor).active = true | |
cautionButton.trailingAnchor.constraintEqualToAnchor(stopButton.trailingAnchor).active = true | |
cautionButton.heightAnchor.constraintEqualToAnchor(stopButton.heightAnchor).active = true | |
cautionButton.centerYAnchor.constraintEqualToAnchor(containerView.centerYAnchor, constant: 0.0).active = true | |
goButton.bottomAnchor.constraintEqualToAnchor(containerView.bottomAnchor, constant: -10).active = true | |
goButton.widthAnchor.constraintEqualToAnchor(stopButton.widthAnchor, multiplier: 0.75).active = true | |
goButton.heightAnchor.constraintEqualToAnchor(stopButton.heightAnchor).active = true | |
goButton.centerXAnchor.constraintEqualToAnchor(containerView.centerXAnchor, constant: 0.0).active = true | |
// This is the old school way to do it. | |
// NSLayoutConstraint(item: goButton, attribute: NSLayoutAttribute.CenterX, relatedBy: .Equal, toItem: containerView, attribute: .CenterX, multiplier: 1.0, constant: 0.0).active = true | |
stopButton.translatesAutoresizingMaskIntoConstraints = false | |
goButton.translatesAutoresizingMaskIntoConstraints = false | |
cautionButton.translatesAutoresizingMaskIntoConstraints = false | |
containerView.layoutIfNeeded() | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment