Skip to content

Instantly share code, notes, and snippets.

@Pasanpr
Created June 2, 2016 15:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Pasanpr/35770b219d0b362350dfd06350106756 to your computer and use it in GitHub Desktop.
Save Pasanpr/35770b219d0b362350dfd06350106756 to your computer and use it in GitHub Desktop.
import UIKit
class ViewController: UIViewController {
@IBOutlet weak var firstView: UIView!
@IBOutlet weak var secondView: UIView!
@IBOutlet weak var thirdView: UIView!
@IBOutlet weak var fourthView: UIView!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
setupConstraints()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
func setupConstraints() {
let topLayoutGuide = UILayoutGuide()
let centerLayoutGuide = UILayoutGuide()
let bottomLayoutGuide = UILayoutGuide()
view.addLayoutGuide(topLayoutGuide)
view.addLayoutGuide(centerLayoutGuide)
view.addLayoutGuide(bottomLayoutGuide)
let firstViewToTopGuideConstraint = NSLayoutConstraint(item: firstView, attribute: .Bottom, relatedBy: .Equal, toItem: topLayoutGuide, attribute: .Top, multiplier: 1.0, constant: 0.0)
let secondViewToTopGuideConstraint = NSLayoutConstraint(item: secondView, attribute: .Top, relatedBy: .Equal, toItem: topLayoutGuide, attribute: .Bottom, multiplier: 1.0, constant: 0.0)
let secondViewToBottomGuideConstraint = NSLayoutConstraint(item: secondView, attribute: .Bottom, relatedBy: .Equal, toItem: centerLayoutGuide, attribute: .Top, multiplier: 1.0, constant: 0.0)
let thirdViewToCenterGuideConstraint = NSLayoutConstraint(item: thirdView, attribute: .Top, relatedBy: .Equal, toItem: centerLayoutGuide, attribute: .Bottom, multiplier: 1.0, constant: 0.0)
let thirdViewToBottomGuideConstraint = NSLayoutConstraint(item: thirdView, attribute: .Bottom, relatedBy: .Equal, toItem: bottomLayoutGuide, attribute: .Top, multiplier: 1.0, constant: 0.0)
let fourthViewToBottomGuideConstraint = NSLayoutConstraint(item: fourthView, attribute: .Top, relatedBy: .Equal, toItem: bottomLayoutGuide, attribute: .Bottom, multiplier: 1.0, constant: 0.0)
let topGuideHeightConstraint = NSLayoutConstraint(item: topLayoutGuide, attribute: .Height, relatedBy: .Equal, toItem: centerLayoutGuide, attribute: .Height, multiplier: 1.0, constant: 0.0)
let centerGuideHeightConstraint = NSLayoutConstraint(item: centerLayoutGuide, attribute: .Height, relatedBy: .Equal, toItem: bottomLayoutGuide, attribute: .Height, multiplier: 1.0, constant: 0.0)
view.addConstraints([firstViewToTopGuideConstraint, secondViewToTopGuideConstraint, secondViewToBottomGuideConstraint, thirdViewToCenterGuideConstraint, thirdViewToBottomGuideConstraint, fourthViewToBottomGuideConstraint, topGuideHeightConstraint, centerGuideHeightConstraint])
}
}
@stoddayy
Copy link

stoddayy commented Apr 3, 2017

Here's the update constraints to work in XCode 8:

    let firstViewToTopGuideConstraint = NSLayoutConstraint(item: firstView, attribute: .bottom, relatedBy: .equal, toItem: topLayoutGuide, attribute: .top, multiplier: 1.0, constant: 0.0)
    
    let secondViewToTopGuideConstraint = NSLayoutConstraint(item: secondView, attribute: .top, relatedBy: .equal, toItem: topLayoutGuide, attribute: .bottom, multiplier: 1.0, constant: 0.0)
    
    let secondViewToBottomGuideConstraint = NSLayoutConstraint(item: secondView, attribute: .bottom, relatedBy: .equal, toItem: centerLayoutGuide, attribute: .top, multiplier: 1.0, constant: 0.0)
    
    let thirdViewToCenterGuideConstraint = NSLayoutConstraint(item: thirdView, attribute: .top, relatedBy: .equal, toItem: centerLayoutGuide, attribute: .bottom, multiplier: 1.0, constant: 0.0)
    
    let thirdViewToBottomGuideConstraint = NSLayoutConstraint(item: thirdView, attribute: .bottom, relatedBy: .equal, toItem: bottomLayoutGuide, attribute: .top, multiplier: 1.0, constant: 0.0)
    
    let fourthViewToBottomGuideConstraint = NSLayoutConstraint(item: fourthView, attribute: .top, relatedBy: .equal, toItem: bottomLayoutGuide, attribute: .bottom, multiplier: 1.0, constant: 0.0)
    
    let topGuideHeightConstraint = NSLayoutConstraint(item: topLayoutGuide, attribute: .height, relatedBy: .equal, toItem: centerLayoutGuide, attribute: .height, multiplier: 1.0, constant: 0.0)
    
    let centerGuideHeightConstraint = NSLayoutConstraint(item: centerLayoutGuide, attribute: .height, relatedBy: .equal, toItem: bottomLayoutGuide, attribute: .height, multiplier: 1.0, constant: 0.0)

@coder110
Copy link

Thanks @stoddayy

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment