Permalink
Switch branches/tags
Nothing to show
Find file Copy path
Fetching contributors…
Cannot retrieve contributors at this time
46 lines (36 sloc) 1.6 KB

autolayout code

// avoid rookie mistake 1:
view.translatesAutoresizingMaskIntoConstraints = false

// avoid rookie mistake 2:
// set your constraints "isActive" property to true
yourView.trailingAnchor.constraint(equalTo: view.trailingAnchor, constant: 100).isActive = true,

// avoid rookie mistake 3:
// remember to set both X and Y constraints
.width, .height, {leading,trailing, centerX}, {top, bottom, centerY}

anchoring horizontally

// centered on X (horizontally)
yourView.centerXAnchor.constraint(equalTo: view.centerXAnchor).isActive = true

// anchor to left+
// some languages read from right to left so apple recommends using
// trailingAnchor instead of leftAnchor
yourView.trailingAnchor.constraint(equalTo: view.trailingAnchor, constant: 100).isActive = true,

// anchor to right+ (notice the negative value)
// some languages read from right to left so apple recommends using
// trailingAnchor instead of rightAnchor
yourView.leadingAnchor.constraint(equalTo: view.leadingAnchor, constant: -100).isActive = true,

anchoring vertically

// centered on Y (vertically)
yourView.centerYAnchor.constraint(equalTo: view.centerYAnchor).isActive = true

// anchor to top+
yourView.topAnchor.constraint(equalTo: view.topAnchor, constant: 100).isActive = true

// anchor to bottom+
yourView.topAnchor.constraint(equalTo: view.topAnchor, constant: 100).isActive = true

avoid rookie mistake #1

link blog