1
votes

Short version: Creating a NSLayoutAttributeCenterX constraint always causes a constraint failure. Why?

I have a UITextField subclass which behaves as a search textbox. You tap the box and a list of items appear, the items are filtered by what you type. Like this:

enter image description here

The UITextField subclass is responsible for creating and displaying the view of items. The center of the list should match the textfield's center. So, I set my constraint like this:

NSLayoutConstraint *hugCenter = 
   [NSLayoutConstraint constraintWithItem:self.searchContainerView
                                attribute:NSLayoutAttributeCenterX
                                relatedBy:NSLayoutRelationEqual
                                   toItem:self
                                attribute:NSLayoutAttributeCenterX
                               multiplier:1.0
                                 constant:0.0];

This invariably leads to the error:

Will attempt to recover by breaking constraint NSLayoutConstraint:0x9a5e1a0 UIView:0x9a5ba40.centerX == SearchTextField:0x7154a40.centerX

For testing I've made my scene as simple as possible -- 1 textfield on a view controller:

enter image description here

Why does this cause a constraint error?

1
Continuing to investigate: this seems to be related to the fact that the textField is centered horizontally in its superview. I still don't understand why this should matter. The list should center on the textField, the textField should center on the superview. I'm not seeing how they are mutually exclusive. - memmons
In the console, right before "Will attempt to recover..", you should have seen a list of all the relevant constraints. Pls copy/paste that into your question. - Firoze Lafeer

1 Answers

0
votes

Are you missing a setTranslatesAutoresizingMaskIntoConstraints: somewhere?