0

出于我正在创建的框架的目的,我需要设置 UIView 对象的框架属性,但是当 AutoLayout 开启时,我不能这样做。我的想法是我可以从 UIView 对象中删除所有布局约束,然后根据我希望它拥有的框架创建新的约束。

所以这就是我所拥有的,但它给我一个错误不起作用(如下)。

//Remove current constraints
[self.view removeConstraints:self.view.constraints];


//create x constraint based on new x value
NSLayoutConstraint *xConstraint =[NSLayoutConstraint
                                   constraintWithItem:self.view
                                   attribute:NSLayoutAttributeLeft
                                   relatedBy:NSLayoutRelationEqual
                                   toItem:self.view.superview
                                   attribute:NSLayoutAttributeLeft
                                   multiplier:1.0
                                   constant:self.x];
// add new x constrain
[self.view.superview addConstraint:xConstraint];

//create y constraint based on new y value
NSLayoutConstraint *yConstraint =[NSLayoutConstraint
                                  constraintWithItem:self.view
                                  attribute:NSLayoutAttributeTop
                                  relatedBy:NSLayoutRelationEqual
                                  toItem:self.view.superview
                                  attribute:NSLayoutAttributeTop
                                  multiplier:1.0
                                  constant:self.y];

[self.view.superview addConstraint:yConstraint];

//create width constraint based on new width value
NSLayoutConstraint *widthConstraint =[NSLayoutConstraint
                                  constraintWithItem:self.view
                                  attribute:NSLayoutAttributeWidth
                                  relatedBy:NSLayoutRelationEqual
                                  toItem:nil
                                  attribute:NSLayoutAttributeWidth
                                  multiplier:1.0
                                  constant:self.width];

[self.view.superview addConstraint:widthConstraint];

//create height constraint based on new height value
NSLayoutConstraint *heightConstraint =[NSLayoutConstraint
                                  constraintWithItem:self.view
                                  attribute:NSLayoutAttributeHeight
                                  relatedBy:NSLayoutRelationEqual
                                  toItem:nil
                                  attribute:NSLayoutAttributeHeight
                                  multiplier:1.0
                                  constant:self.height];

[self.view.superview addConstraint:heightConstraint];

这是运行时发生的情况:

编辑

更改[self.view addConstraint:heightConstraint];[self.view.superview addConstraint:heightConstraint];元素显示后,但我在控制台中收到以下消息

错误

以下列表中的至少一个约束可能是您不想要的。试试这个:(1)查看每个约束并尝试找出您不期望的;(2) 找到添加了一个或多个不需要的约束的代码并修复它。(注意:如果您看到不理解的 NSAutoresizingMaskLayoutConstraints,请参阅 UIView 属性 translatesAutoresizingMaskIntoConstraints 的文档)

(
    "<NSLayoutConstraint:0x91475c0 H:|-(1)-[UIButton:0x728ab00](LTR)   (Names: '|':UIScrollView:0x72845b0 )>",
    "<NSLayoutConstraint:0x729dad0 H:|-(25)-[UIImageView:0x7284920](LTR)   (Names: '|':UIScrollView:0x72845b0 )>",
    "<NSLayoutConstraint:0x72924d0 UIButton:0x728ab00.leading == UIImageView:0x7284920.leading>"
)
4

1 回答 1

0

您的问题是您正在向 self.view 添加一个引用 self.view.superview 的约束。这将不起作用,这就是您看到“约束是否从视图的子树外部引用某些内容?这是非法的”的原因。

你到底想做什么?必须有另一种方法来解决这个问题。可能您可以将约束添加到超级视图,但我不完全确定您的情况。

于 2013-08-14T22:39:55.547 回答