Skip to content

Instantly share code, notes, and snippets.

@thanhluu
Created June 24, 2016 12:13
Show Gist options
  • Save thanhluu/eccfab401a6a2b51bdfbb45c319b4e0e to your computer and use it in GitHub Desktop.
Save thanhluu/eccfab401a6a2b51bdfbb45c319b4e0e to your computer and use it in GitHub Desktop.
BaiTapAutoLayout
class MyViewController: UIViewController {
let sampleView = UIView()
override func viewDidLoad() {
super.viewDidLoad()
view.addSubview(sampleView)
}
override func viewWillLayoutSubviews() {
super.viewWillLayoutSubviews()
sampleView.translatesAutoresizingMaskIntoConstraints = false
// Add constraints below
let sampleViewHeightConstraint = NSLayoutConstraint(item: sampleView, attribute: .Height, relatedBy: .Equal, toItem: nil, attribute: .NotAnAttribute, multiplier: 1.0, constant: 50.0)
let sampleViewWidthConstraint = NSLayoutConstraint(item: sampleView, attribute: .Width, relatedBy: .Equal, toItem: nil, attribute: .NotAnAttribute, multiplier: 1.0, constant: 50.0)
let sampleViewCenterXConstraint = NSLayoutConstraint(item: sampleView, attribute: .CenterX, relatedBy: .Equal, toItem: view, attribute: .CenterX, multiplier: 1.0, constant: 0.0)
let sampleViewCenterYConstraint = NSLayoutConstraint(item: sampleView, attribute: .CenterY, relatedBy: .Equal, toItem: view, attribute: .CenterY, multiplier: 1.0, constant: 0.0)
view.addConstraints([
sampleViewHeightConstraint,
sampleViewWidthConstraint,
sampleViewCenterXConstraint,
sampleViewCenterYConstraint
])
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment