Created
December 20, 2014 10:50
-
-
Save jacobjiangwei/64cf142c17a24cbbaa89 to your computer and use it in GitHub Desktop.
Autolayout Programmatically
This file contains 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
Normally we use autolayout in Storyboard in iOS. | |
It's cool and easy to use , what you see is what you get. | |
Somehow some project's nerd don't think so , they build a project using programmaticlly autolayout. | |
So here i am , i have to do the home work to find out exactly how to build using Visual Format syntax. | |
This is a demo , just copy to a new created project : | |
let testView = UIView() | |
testView.backgroundColor=UIColor.redColor() | |
self.view.addSubview(testView) | |
testView.setTranslatesAutoresizingMaskIntoConstraints(false) | |
testView.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("H:[testView(width)]", options:nil, metrics: ["width":50], views: ["testView":testView])) | |
testView.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("V:[testView(height)]", options: nil, metrics: ["height":100], views: ["testView":testView])) | |
self.view.addConstraint(NSLayoutConstraint(item: testView, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem:self.view, attribute: NSLayoutAttribute.CenterX, multiplier: 1.0, constant: 0.0)) | |
self.view.addConstraint(NSLayoutConstraint(item: testView, attribute: NSLayoutAttribute.CenterY, relatedBy: NSLayoutRelation.Equal, toItem: self.view, attribute: NSLayoutAttribute.CenterY, multiplier: 1.0, constant: 0.0)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment