Centering subview's X in autolayout throws “not prepared for the constraint”

那年仲夏 提交于 2019-11-27 03:35:24

As the error states, you must add the constraint to a view such that the views involved in the constraint are the view you're adding it to or a subview of that view. In the case of your code above, you should be adding that constraint to self, rather than [self internalView]. The reason it doesn't work as written is one of the views involved in the constraint (self) is not in the view hierarchy if you consider only [self internalView] and below).

For more details, see the discussion section of the documentation on the addConstraint: method.

Here is your line of code, fixed as I suggest:

UIView* internalView = [[UIView alloc] initWithFrame:CGRectZero];
internalView.translatesAutoresizingMaskIntoConstraints = NO;
[self setInternalView:internalView];

[self addConstraint: [NSLayoutConstraint constraintWithItem: [self internalMapView]
                                         attribute: NSLayoutAttributeCenterX
                                         relatedBy: NSLayoutRelationEqual
                                         toItem: self
                                         attribute: NSLayoutAttributeCenterX
                                         multiplier: 1
                                         constant: 0]];

For others who come here: I got this error, because I added the constraints before I added the subviews to the hierarchy.

Short answer: swap parent and child view.
Assuming self is the parent view:

  • bad: [subview addConstraint [NSLayoutConstraint constraintWithItem:self...

  • good: [self addConstraint [NSLayoutConstraint constraintWithItem:subview...


Swift

subview.translatesAutoresizingMaskIntoConstraints = false
self.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat(
    "H:|-0-[subview]-0-|",
    options: .DirectionLeadingToTrailing,
    metrics: nil,
    views: ["subview":subview]))

Obj-C

[subview setTranslatesAutoresizingMaskIntoConstraints:NO];
[self addConstraints:[NSLayoutConstraint
                      constraintsWithVisualFormat:@"H:|-0-[subview]-0-|"
                      options:NSLayoutFormatDirectionLeadingToTrailing
                      metrics:nil
                      views:NSDictionaryOfVariableBindings(subview)]];

Additional Tips:

I have a newly developed App which running fine on iOS7 but get error on iOS8 "The view hierarchy is not prepared for the constraint".

Read some other post said that the (sub)views need to be added before create constraints. I did that but still got the error.

Then I figured out that I should create constraints under -(void)viewDidAppear instead of viewDidLoad

This is an old question but I want to point out this line from the docs:

When developing for iOS 8.0 or later, set the constraint’s isActive property to true instead of calling the addConstraint(_:) method directly. The isActive property automatically adds and removes the constraint from the correct view.

At the very least, this will remove one point of failure since you no longer need to worry about calling addConstraint on the wrong view

In my case, it was solved by using root view as a container of constraints instead of a currently created view.

I mean, use inside viewController

view.addConstraints([leftConstraintImage, topConstraintImage]) instead of imageView.addConstraints...

As @CharlesA points out in his answer the problem is that the view you are adding is not in the proper view hierarchy. His answer is a good one, but I'm going to throw in some detail for a particular use-case that caused this problem for me:

I had this happen when attempting to add constraints to a view whose view controller I had instantiated using instantiateViewControllerWithIdentifier. To solve the problem, I used [childViewController didMoveToParentViewController:self]. This added the view into the view hierarchy that was referenced by my constraint.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!