Sunday, November 13, 2016

Layout Anchors

Why describe what layout anchors are when you can show it!
These are the two UIViews I will be demonstrating.
Technically this example is not using a Layout Anchor but it is a good way to center objects.
 
myView.center = view.center
 
IMPORTANT! All examples below will need to include these 3 lines of code for them to work.
 
myView.translatesAutoresizingMaskIntoConstraints = false
myView.widthAnchor.constraint(equalToConstant: 125).isActive = true
myView.heightAnchor.constraint(equalToConstant: 125).isActive = true
 
Without setting translatesAutoresizingMaskIntoConstraints to false, the new constraints will do nothing.
Without a height and width I found a UIView will just collapse into no height or width.

Horizontal Examples

 
myView.leftAnchor.constraint(equalTo: view.leftAnchor).isActive = true
myView.leadingAnchor.constraint(equalTo: view.leadingAnchor).isActive = true
 

 
myView.rightAnchor.constraint(equalTo: view.rightAnchor).isActive = true
 

 
myView.rightAnchor.constraint(equalTo: view.leftAnchor, constant: 30).isActive = true
 

 
// Offset from center -50
myView.centerXAnchor.constraint(equalTo: view.centerXAnchor, constant: -50).isActive = true
 

Vertical Examples

 
// Going to center myView for the Vertical examples.
myView.centerXAnchor.constraint(equalTo: view.centerXAnchor).isActive = true
myView.topAnchor.constraint(equalTo: view.topAnchor).isActive = true
 

 
myView.bottomAnchor.constraint(equalTo: view.bottomAnchor).isActive = true
 

(Xcode 8, Swift 3.0)

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.

SwiftUI Search & Filter with Combine - Part 3 (iOS, Xcode 13, SwiftUI, 2...

In part 3 of the Searchable video series, I show you how to use Combine in #SwiftUI for the search and filter logic connected to the searcha...