如何子视图相对于包含视图大小的位置? [英] How to position a child view relative to containing view size?

查看:179
本文介绍了如何子视图相对于包含视图大小的位置?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望能够定位我的孩子视图25%从顶超级视图的大小。

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?

推荐答案

您不能使用在相同的约束顶部和高度。虽然它是有道理的说,它的系统不喜欢它。

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];

这篇关于如何子视图相对于包含视图大小的位置?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆