Tutorial by Examples

Boilerplate code example override func viewDidLoad() { super.viewDidLoad() let myView = UIView() myView.backgroundColor = UIColor.blueColor() myView.translatesAutoresizingMaskIntoConstraints = false view.addSubview(myView) // Add constraints code here // ... ...
Auto layout is used to arrange views so that they look good on any device and orientation. Constraints are the rules that tell how everything should be laid down. They include pinning edges, centering, and setting sizes, among other things. Auto layout is enabled by default, but you can double chec...
Select your button (or whatever view you want to center) on the storyboard. Then click the align button on the bottom right. Select Horizontally in Container and Vertically in Container. Click "Add 2 Constraints". If it wasn't perfectly centered already, you may need to do one more thin...
It is common to want two views to be side by side, centered in their superview. The common answer given on Stack Overflow is to embed these two views in a UIView and center the UIView. This is not necessary or recommended. From the UILayoutGuide docs: There are a number of costs associated with...
We have to create a view which will have a image prefix to a text. text could be of variable length.We have to achieve a result where in Image + text is always in center of a parent view. Step 1: First create a single view project and name it something of your choice and open the story board fist...
Without Auto Layout, animation is accomplished changing a view's frame over time. With Auto Layout, the constraints dictate the view frame, so you have to animate the constraints instead. This indirection makes animation harder to visualize. Here are the ways to animate with Auto Layout: Change ...
Problem: When you use many labels inside a view, you maybe get a warning: How can we fix this warning? Solution: We calculate and set the priorities in order. The priorities must be different from labels. It means which is important will get higher priority. For example, in my case, I set the ve...
Step by Step Guide :- Step 1 :- Set constraint to UIView Leading. 2) Top. 3) Trailing. (From mainview) Step 2 :- Set constrain to Label 1 Leading 2) Top 3) Trailing (From it's superview) Step 3 :- Set constraint to Label 2 Leading 2) Top 3) Trailing (From its superview) Step...
HVFL is a language designed to constrain UI elements in a simple and quick fashion. Generally, VFL has an advantage over traditional UI customization in the Interface Builder because it's much more readable, accessible and compact. Here's an example of VFL, in which three UIViews are constrained f...
Sometimes you may want to perform some additional actions to Auto Layout calculations done by UIKit itself. Example: when you have a UIView that has a maskLayer, you may need to update maskLayer as soon as Auto Layout changes UIView's frame // CustomView.m - (void)layoutSubviews { [super la...
Constraint created as NSLayoutConstraint(item: myView, attribute: NSLayoutAttribute.Leading, relatedBy: NSLayoutRelation.Equal, toItem: view, attribute: NSLayoutAttribute.LeadingMargin, multiplier: 1.0, constant: 20.0) or, from math point of view: view.attribute * multiplier + constant ...
When we are working on a framework, if the constraints are not too complex, we'd better use Interface Builder or NSLayoutConstraint in code to make it smaller enough, instead of import Masonry or SnapKit. for example: Objective-C // 1. create views UIView *blueView = [[UIView alloc...

Page 1 of 1