Search code examples
iosswiftios11nslayoutconstraintnslayoutanchor

Console warning when using NSLayoutAnchor.constraintEqualToSystemSpacingAfter


I am using NSLayoutAnchor's constraintEqualToSystemSpacingAfter for building my layout.

NSLayoutConstraint.activate([
    customView.leadingAnchor.constraintEqualToSystemSpacingAfter(safeAreaLayoutGuide.leadingAnchor, multiplier: 1)
])

It actually works, but it throws a warning in the console after I activate the constraints:

Aligning the right edge of a [custom view] with the right edge of a [second custom view] is not recommended. Use an explicit constant for your constraint to override this.

How can I make this warning disappear?


Solution

  • Use NSLayoutAnchor.constraint(equalTo:constant:) instead of that method.

    NSLayoutConstraint.activate([
        customView.leadingAnchor.constraint(equalTo: safeAreaLayoutGuide.leadingAnchor, constant: 0)
    ])