How to position a child view relative to containing view size?

autolayout, ios

Solution

You can't use top and height in the same constraint. Although it makes sense to say it the system doesn't like it.

What you could do instead is something like...

NSLayoutConstraint *topPositionConstraint =
    [NSLayoutConstraint  constraintWithItem:_childView
                                  attribute:NSLayoutAttributeTop 
                                  relatedBy:NSLayoutRelationEqual 
                                     toItem:_containerView
                                  attribute:NSLayoutAttributeTop 
                                 multiplier:1.0 
                                   constant:_containerView.frame.size.height * 0.25];

Problem

I want to be able to position my child view 25% the size of the super view from the top. ``` NSLayoutConstraint *topPositionConstraint = [NSLayoutConstraint constraintWithItem:_containerView attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:_childView attribute:NSLayoutAttributeHeight multiplier:0.25f constant:0.0f]; ``` However, right now I'm getting the following exception: ``` 'NSInvalidArgumentException', reason: '*** +[NSLayoutConstraint constraintWithItem:attribute:relatedBy:toItem:attribute:multiplier:constant:]: Invalid pairing of layout attributes' ``` Why does the error occur and how can I achieve what I want?

Original source