Self Taught Swift: Aligning Objects Programmatically

All you need is X, Y, and center

4 min readJan 31, 2016

--

Every single visual element that you will use to build your application has coordinates built into it:

MinX, midX, maxX, minY, midY, maxY are read-only properties. Meaning we can see what they equal but we can’t change them. X, y, center, center.y, center.x are read-write properties. Meaning we can see what they equal and we can change them.

We can work with all of these properties just like this:

let label = UILabel()
label.frame.minX
label.frame.midX
label.frame.maxX
label.frame.minY
label.frame.midY
label.frame.maxY
label.frame.origin.y
label.frame.origin.x
label.center
label.center.x
label.center.y

Everything is relative. When you’re aligning an object you’re either aligning it relative to the dimensions of the screen or relative to another object. Imagine arranging furniture in a room — Put the couch against the left wall / Put the couch along the left side of the recliner.

To place a label (add elements without using storyboard) in the very center of a viewController screen you just match up their…

--

--