具有常数的高度约束不是“稳定的”(Height constraint with a constant is not “stable”)

编程入门 行业动态 更新时间:2024-10-28 02:35:48
具有常数的高度约束不是“稳定的”(Height constraint with a constant is not “stable”)

我正在添加一个高度约束(设置为300px ),如下所示:

[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在tap to tap之间改变(我正在控制值)。

问题:

在第一次点击期间,containerView_的高度从300变为299(randomaly!),然后又变回300并且看起来不太好(看起来视图正在增长和缩小)。

我通过覆盖layoutSubviews和打印containerView_的框架来验证这layoutSubviews 。

什么可能导致高度从300变为299,即使我设置了300的约束(没有其他!)?

I'm adding a height constraint (set to 300px) as follows:

[parentView_ addConstraints:@[ [NSLayoutConstraint constraintWithItem:containerView_ attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutAttributeNotAnAttribute multiplier:1.0f constant:300], ]];

parentView_ has a subview called containerView_.

containerView_ has two subviews: view1 and view2

I then add button that invokes the following function once tapped:

[containerView_ removeConstraints:[containerView_ constraints]]; [containerView_ addConstraints:@[ [NSLayoutConstraint constraintWithItem:view1 attribute:NSLayoutAttributeCenterY relatedBy:NSLayoutRelationEqual toItem:view2 attribute:NSLayoutAttributeBottom multiplier:someVal1 constant:someVal2], ]];

someVal1 and someVal2 change between tap to tap (I'm controlling the values).

Problem:

During the first taps, the height of containerView_ changes from 300 to 299 (randomaly!), and then back to 300 and it doesn't look good (looks like the view is growing and shrinking).

I've verified this by overriding layoutSubviews and printing containerView_'s frame.

What might cause the height to change from 300 to 299, even though I've set a contraint that says 300 (and nothing else!)?

最满意答案

示例项目包含已应用的约束的完整列表,其中包括:

SampleSlider.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):

<SampleSlider: 0x8988bd0; frame = (12 -1; 56 301); layer = <CALayer: 0x894e4b0>> <SampleSlider: 0x894f260; frame = (12 1; 56 299); layer = <CALayer: 0x894b770>>

注意y位置 - 它不是保持稳定在0,而是分别跳到-1,1和高度(301,299)。 抛开y定位问题,框架的高度是真正令人烦恼的:

虽然使用了以下线性方程(或者看起来如此?),但它不能以任何方式满足约束条件:

frameHeight = constraintHeight - y

如:

301 = 300 - (-1)

(所以实际高度是abs(-1)+ 301 = 302)

对于上面的第一帧,和,

299 = 300 - 1

(所以实际高度是299 - 1 = 298)

对于第二帧。 闻起来好笑,不是吗? 似乎有人犯了一个错误并减去了y值而不是添加它。

将等式(假设意义)改为

frameHeight = constraintHeight + y

产量

frameHeight = 300 + -1 = 299

对于第一帧,所以实际高度现在为300(y = -1,h = 299)

frameHeight = 300 + 1 = 301

对于第二帧,所以实际高度再次为300(y = 1,h = 301)

Sample project contains the full list of constraints that were applied, amongst them:

SampleSlider.m:59

[NSLayoutConstraint constraintWithItem:slider_ attribute:NSLayoutAttributeCenterY relatedBy:NSLayoutRelationEqual toItem:self attribute:NSLayoutAttributeCenterY multiplier:1.0 constant:0]

Changing this constraint to

[NSLayoutConstraint constraintWithItem:slider_ attribute:NSLayoutAttributeBottom relatedBy:NSLayoutRelationEqual toItem:self attribute:NSLayoutAttributeBottom multiplier:1.0 constant:0]

Solved the issue. It appears as a CenterY constraint isn't precise, or floor()'d/ceil()'d to different values on each run.

The funny thing I found is as follows: During two different runs I printed out the frame (who's height was supposed to be 300):

<SampleSlider: 0x8988bd0; frame = (12 -1; 56 301); layer = <CALayer: 0x894e4b0>> <SampleSlider: 0x894f260; frame = (12 1; 56 299); layer = <CALayer: 0x894b770>>

Notice the y position - instead of staying steady at 0, it jumps around to -1, 1 Alongside with the height (301, 299) respectively. Set aside the y positioning issue, the frame's height is what's really bothering:

Although the following linear equation is used (or so it appears?), it does not satisfy the constraint in any way:

frameHeight = constraintHeight - y

As in:

301 = 300 - (-1)

(so actual height is abs(-1) + 301 = 302)

for the first frame above, and,

299 = 300 - 1

(so actual height is 299 - 1 = 298)

For the second frame. Smells funny, doesn't it? seems like someone made a mistake and subtracted the y value instead of adding it.

Changing the equation (hypothetically meaning) to

frameHeight = constraintHeight + y

Yields

frameHeight = 300 + -1 = 299

for the first frame, so actual height is now 300 (y = -1, h = 299)

frameHeight = 300 + 1 = 301

for the second frame, so actual height is 300 again (y = 1, h = 301)

更多推荐

本文发布于:2023-07-23 01:17:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1225561.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:常数   高度   稳定   Height   constant

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!