2

I have a UIViewController, and I want to update another view within the controller so that the leading space is a certain distance from the left margin.

My question is how do I get the size of the margin or how do I position something with respect to the margin?

rob mayoff
  • 375,296
  • 67
  • 796
  • 848
App Face
  • 81
  • 5

1 Answers1

2

The view's left margin size is view.layoutMargins.left.

You can get a layout anchor for the left margin as view.layoutMarginsGuide.leftAnchor. Example:

subview.leftAnchor.constraintEqualToAnchor(view.layoutMargins.leftAnchor).active = true

Note though that if you want your interface to flip correctly in a right-to-left locale, you should use leadingAnchor:

subview.leadingAnchor.constraintEqualToAnchor(view.layoutMargins.leadingAnchor).active = true

If you want to create a layout constraint using the old NSLayoutConstraint. init(item:attribute:relatedBy:toItem:attribute:multiplier:constant:) constructor, the attributes are named .LeftMargin and .LeadingMargin.

rob mayoff
  • 375,296
  • 67
  • 796
  • 848