5

My goal : to center an indicator ( vertically and horizontally ) to the button in a view

What I am doing is :

[self.logInButton addConstraint:[NSLayoutConstraint constraintWithItem:spinner1
                                                             attribute:NSLayoutAttributeCenterY
                                                             relatedBy:NSLayoutRelationEqual
                                                                toItem:self.logInButton
                                                             attribute:NSLayoutAttributeCenterY
                                                            multiplier:1.0
                                                              constant:0]];
[self.logInButton addConstraint:[NSLayoutConstraint constraintWithItem:spinner1
                                                             attribute:NSLayoutAttributeCenterX
                                                             relatedBy:NSLayoutRelationEqual
                                                                toItem:self.logInButton
                                                             attribute:NSLayoutAttributeCenterX
                                                            multiplier:1.0
                                                              constant:0]];
self.logInButton addSubView:spinner

However, I am getting a warning below

The view hierarchy is not prepared for the constraint: <NSLayoutConstraint:0x17e19390 UIActivityIndicatorView:0x17d035d0.centerY == UIButton:0x17df4430.centerY>
    When added to a view, the constraint's items must be descendants of that view (or the view itself). This will crash if the constraint needs to be resolved before the view hierarchy is assembled. Break on -[UIView _viewHierarchyUnpreparedForConstraint:] to debug.
2013-06-12 17:37:19.610 FlipGive[366:60b] The view hierarchy is not prepared for the constraint: <NSLayoutConstraint:0x17e19810 UIActivityIndicatorView:0x17d035d0.centerX == UIButton:0x17df4430.centerX>
    When added to a view, the constraint's items must be descendants of that view (or the view itself). This will crash if the constraint needs to be resolved before the view hierarchy is assembled. Break on -[UIView _viewHierarchyUnpreparedForConstraint:] to debug.

Does anyone know what I am doing wrong here ? Please help

All comments are welcomed here. Thanks

tranvutuan
  • 6,089
  • 8
  • 47
  • 83
  • Add the constraints to the superview of both the button and the spinner. – Jano Jun 12 '13 at 21:46
  • are there any explanations for your comment ? I just want to understand deely – tranvutuan Jun 12 '13 at 21:57
  • All the views a constraint references must be in the subtree of the view it is being added to. In general, add the constraints to the nearest ancestor for better performance, or to the component it logically belongs to (the login button in this case). – Jano Jun 28 '13 at 15:25

1 Answers1

24

You need to add the views before adding constraints.

put this first and then add constraints

self.logInButton addSubView:spinner
THE_DOM
  • 4,266
  • 1
  • 18
  • 18