Ios 带有常量的高度约束不是“高度约束”;“稳定”;
我正在添加一个高度约束(设置为300px),如下所示:Ios 带有常量的高度约束不是“高度约束”;“稳定”;,ios,iphone,objective-c,ipad,autolayout,Ios,Iphone,Objective C,Ipad,Autolayout,我正在添加一个高度约束(设置为300px),如下所示: [parentView_ addConstraints:@[ [NSLayoutConstraint constraintWithItem:containerView_ attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEq
[parentView_ addConstraints:@[
[NSLayoutConstraint constraintWithItem:containerView_
attribute:NSLayoutAttributeHeight
relatedBy:NSLayoutRelationEqual
toItem:nil attribute:NSLayoutAttributeNotAnAttribute
multiplier:1.0f constant:300],
]];
parentView
有一个子视图,名为containerView
containerView
有两个子视图:view1
和view2
然后,我添加一个按钮,点击该按钮后调用以下函数:
[containerView_ removeConstraints:[containerView_ constraints]];
[containerView_ addConstraints:@[
[NSLayoutConstraint constraintWithItem:view1
attribute:NSLayoutAttributeCenterY
relatedBy:NSLayoutRelationEqual
toItem:view2 attribute:NSLayoutAttributeBottom
multiplier:someVal1 constant:someVal2],
]];
someVal1
和someVal2
在点击之间切换(我控制数值)
问题:
在第一次点击期间,containerView的高度从300变为299(随机!),然后又变回300,看起来不太好(看起来视图正在增长和缩小)
我通过覆盖layoutSubviews
并打印containerView
的框架来验证这一点
是什么原因导致高度从300变为299,即使我设置了一个约束,上面写着300(没有其他!)?示例项目包含应用的约束的完整列表,其中包括: m:59
[NSLayoutConstraint constraintWithItem:slider_ attribute:NSLayoutAttributeCenterY relatedBy:NSLayoutRelationEqual
toItem:self attribute:NSLayoutAttributeCenterY
multiplier:1.0 constant:0]
将此约束更改为
[NSLayoutConstraint constraintWithItem:slider_ attribute:NSLayoutAttributeBottom relatedBy:NSLayoutRelationEqual
toItem:self attribute:NSLayoutAttributeBottom
multiplier:1.0 constant:0]
解决了这个问题。它显示为CenterY约束不精确,或floor()'d/ceil()'d在每个梯段上的值不同
我发现有趣的事情如下:在两次不同的跑步中,我打印出了相框(谁的身高应该是300):
例如:
301 = 300 - (-1)
(因此实际高度为abs(-1)+301=302)
对于上面的第一帧,以及
299 = 300 - 1
(因此实际高度为299-1=298)
第二帧。
闻起来很好笑,不是吗?好像有人犯了个错误,把y值减了,而不是加了
将等式(假设意义)更改为
屈服
frameHeight=300+-1=299
对于第一帧,实际高度现在是300(y=-1,h=299)
frameHeight=300+1=301
对于第二帧,因此实际高度再次为300(y=1,h=301)您可以发布一个显示此问题的示例项目吗?@ArkadiuszHolko示例项目可以找到。它可能不会在第一次运行时复制,因此请先对其进行几次拍摄,直到您在控制台中看到“检测到错误的高度”@ArkadiuszHolko忘记提及:右侧有一个蓝色的UISlider-尝试移动它,它会触发一个约束移除/添加,从而导致高度发生变化。@ArkadiuszHolko问题已解决:-)
301 = 300 - (-1)
299 = 300 - 1
frameHeight = constraintHeight + y