Skip to content

Instantly share code, notes, and snippets.

@Pasanpr Pasanpr/blueView.swift Secret
Created May 31, 2016

Embed
What would you like to do?
func setupBlueViewConstraints() {
blueView.translatesAutoresizingMaskIntoConstraints = false
let blueViewTopSpaceConstraint = NSLayoutConstraint(item: blueView, attribute: .Top, relatedBy: .Equal, toItem:view, attribute: .Top, multiplier: 1.0, constant: 8)
let blueViewBottomSpaceConstraint = NSLayoutConstraint(item: blueView, attribute: .Bottom, relatedBy: .Equal, toItem: orangeView, attribute: .Top, multiplier: 1.0, constant: -8.0)
let blueViewTrailingSpaceConstraint = NSLayoutConstraint(item: blueView, attribute: .Trailing, relatedBy: .Equal, toItem: view, attribute: .TrailingMargin, multiplier: 1.0, constant: 8.0)
let equalWidthConstraints = NSLayoutConstraint(item: blueView, attribute: .Width, relatedBy: .Equal, toItem: purpleView, attribute: .Width, multiplier: 1.0, constant: 0.0)
view.addConstraints([blueViewTopSpaceConstraint, blueViewBottomSpaceConstraint, blueViewTrailingSpaceConstraint, equalWidthConstraints])
}
@davejlin

This comment has been minimized.

Copy link

davejlin commented Oct 2, 2016

There's a typo for blueViewTrailingSpaceConstraint:
The constant should be -8.0, not 8.0.

Incorrect:

let blueViewTrailingSpaceConstraint = NSLayoutConstraint(item: blueView, attribute: .Trailing, relatedBy: .Equal, toItem: view, attribute: .TrailingMargin, multiplier: 1.0, constant: 8.0)

Correct:

let blueViewTrailingSpaceConstraint = NSLayoutConstraint(item: blueView, attribute: .Trailing, relatedBy: .Equal, toItem: view, attribute: .TrailingMargin, multiplier: 1.0, constant: -8.0)
@garciadanny

This comment has been minimized.

Copy link

garciadanny commented Dec 7, 2016

Thanks @davejlin

@NFulkerson

This comment has been minimized.

Copy link

NFulkerson commented Mar 7, 2017

It's a trivial change for Swift 3 and shouldn't cause any trouble for students to go through and fix, but Xcode complains loudly about the enum casing. The Swift 3 equivalent is:

func setupBlueViewConstraints() {
        
        blueView.translatesAutoresizingMaskIntoConstraints = false
        
        let blueViewTopSpaceConstraint = NSLayoutConstraint(item: blueView, attribute: .top, relatedBy: .equal, toItem:view, attribute: .top, multiplier: 1.0, constant: 8)
        let blueViewBottomSpaceConstraint = NSLayoutConstraint(item: blueView, attribute: .bottom, relatedBy: .equal, toItem: orangeView, attribute: .top, multiplier: 1.0, constant: -8.0)
        let blueViewTrailingSpaceConstraint = NSLayoutConstraint(item: blueView, attribute: .trailing, relatedBy: .equal, toItem: view, attribute: .trailingMargin, multiplier: 1.0, constant: -8.0)
        let equalWidthConstraints = NSLayoutConstraint(item: blueView, attribute: .width, relatedBy: .equal, toItem: purpleView, attribute: .width, multiplier: 1.0, constant: 0.0)
        
        view.addConstraints([blueViewTopSpaceConstraint, blueViewBottomSpaceConstraint, blueViewTrailingSpaceConstraint, equalWidthConstraints])
    }
@iamGus

This comment has been minimized.

Copy link

iamGus commented Jul 11, 2017

Thanks @NFulkerson

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.