"Cannot convert value of type 'NSLayoutDimension' to expected argument type NSLayoutAnchor<NSLayoutXAxisAnchor>

Thalatta picture Thalatta · Jan 16, 2017 · Viewed 8.3k times · Source

I am trying to add a subview in my Swift 3 tableView. I want the leading anchor to be related to 0.05 x the width of my parent view( tableView).

When I write the following:

modalView.leadingAnchor.constraint(equalTo:margins.widthAnchor, constant: 0.05).isActive = true

I get

Cannot convert value of type 'NSLayoutDimension' to expected argument type 'NSLayoutAnchor<NSLayoutXAxisAnchor>'

How can I create the desired leading constraint using NSLayoutAnchor?

Answer

mfaani picture mfaani · Oct 24, 2017

I just had a similar issue. I was getting:

Cannot convert value of type 'NSLayoutYAxisAnchor' to expected argument type 'NSLayoutAnchor <\NSLayoutXAxisAnchor>'

X and Y are two different axes

If your doing something related to 'top and bottom', then the things you want to constrain against each other should both belong to the same AXIS! Meaning you can't constrain 'Left to top'! You can't constraint 'bottom to left'. They have to be parallel!

your case is different, but still the root cause is the same. That is you can't say "constrain the size of an element to a point".

Similarly you can't constraint a width to an axis. Width can be constrained with another dimension i.e. it could be constrained to height or width or depth or just a specific value like 20.0