Iphone 在运行时更改元素的左侧、顶部、宽度和高度约束
对于我正在创建的框架,我需要设置UIView对象框架属性,但当“自动布局”处于启用状态时,我无法进行设置。 我的想法是,我可以从UIView对象中删除所有布局约束,然后根据我想要的框架创建新的约束 这就是我所拥有的,但它不起作用,给我一个错误(如下) 这是运行时发生的情况: 编辑 更改Iphone 在运行时更改元素的左侧、顶部、宽度和高度约束,iphone,ios,objective-c,constraints,autolayout,Iphone,Ios,Objective C,Constraints,Autolayout,对于我正在创建的框架,我需要设置UIView对象框架属性,但当“自动布局”处于启用状态时,我无法进行设置。 我的想法是,我可以从UIView对象中删除所有布局约束,然后根据我想要的框架创建新的约束 这就是我所拥有的,但它不起作用,给我一个错误(如下) 这是运行时发生的情况: 编辑 更改[self.view addConstraint:heightConstraint]后至[self.view.superview addConstraint:heightConstraint]元素显示,但我在控制台
[self.view addConstraint:heightConstraint]后代码>至[self.view.superview addConstraint:heightConstraint]代码>元素显示,但我在控制台中收到以下消息
错误
可能下面列表中至少有一个约束是您不想要的。试着这样做:(1)看看每个约束,试着找出你不期望的约束;(2) 找到添加了不需要的约束的代码,然后修复它。(注意:如果您看到不理解的NSAutoresizingMaskLayoutConstraints,请参阅UIView属性TranslatesAutoResizingMaskToConstraints的文档)
(
"",
"",
""
)
您的问题是您正在向self.view添加一个引用self.view.superview的约束。这将不起作用,这就是您看到“约束是否引用了视图子树之外的内容?这是非法的”的原因
你到底想干什么?必须有另一种方法来解决这个问题。您可能会将约束添加到superview,但我不能完全确定您的情况。将约束添加到superview会修复该缺陷,但不会修复根据新的x、y、宽度参照其super view定位视图的概念,封装为约束的高度数据您希望self.view如何定位在其superview中,现在得到什么结果?刚刚编辑了问题。在我将约束添加到superview之后,对象的位置与我预期的一样,但是控制台中的附加信息表明我没有做正确的事情。我在问题中发布了控制台信息。查看您的编辑,我发现您的新问题是由于您为子视图(滚动视图中按钮和图像的位置)创建的限制。从输出来看,我认为您的问题在于您将按钮和图像的水平位置设置为与滚动视图左边缘不同的距离。然后尝试将按钮的前缘与图像对齐。这些说明相互冲突。从注释出前缘约束开始,您不应该看到错误。谢谢,我知道在您的编辑中也会查找什么:正如您的错误所提示的,可能有NSAutoResizengMaskLayoutConstraints与您手动创建的约束冲突。您需要将self.view的translatesAutoresizingMaskIntoConstraints属性设置为NO以防止此问题。编辑:刚刚看到您的第二次编辑,此注释不正确。旁注:我认为对于高度约束,您应该将第二个属性设置为nsLayoutAttribute。
//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];
(
"<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>"
)