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.
Without a height and width I found a UIView will just collapse into no height or width.
(Xcode 8, Swift 3.0)
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.centerIMPORTANT! 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 = trueWithout 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.