iOS UITableviewcell 中的宽高比约束以编程方式被打破
iOS programmatically aspect ratio constraint gets broken in UITableviewcell
我正在尝试为 tableviewcell 中的图像设置纵横比,但由于 iOS 无法使用不同于 .5 或 . 0.
如果约束接近其值,或者它被四舍五入为擦除小数点的值,那么有什么办法可以使约束不 fail/break?
经过多次尝试,我知道问题是因为小数,我将通过以下示例向您展示:
用下面的代码
postImage.heightAnchor.constraint(equalToConstant: 88.9).isActive = true
我收到以下错误:
Will attempt to recover by breaking constraint
<NSLayoutConstraint:0x6000036067b0 UIImageView:0x7fb76fe397e0.height == 88.9 (active)>
而视图视图层级图像的大小是89
当我设置不同于 .5 或 .0 的高度时,它会四舍五入为最接近的值 .5 或 .0(如果我设置 88.6,它会四舍五入为 88.5 并抛出错误),但如果我设置任何以 .0 或 .5 结尾的值,它就可以正常工作!
(我知道我可以设置较低的优先级,但它会给我带来更多意想不到的行为)
(我正在以编程方式执行此操作,通过 Xib 执行此操作不是一个选项:))
编辑:整个日志:
(
"<NSLayoutConstraint:0x600000facd20 UIImageView:0x7fc98362fe80.height == 86.9 (active)>",
"<NSLayoutConstraint:0x600000facd70 V:|-(0)-[UIImageView:0x7fc98362fe80] (active, names: '|':UITableViewCellContentView:0x7fc98362f940 )>",
"<NSLayoutConstraint:0x600000face10 UIImageView:0x7fc98362fe80.bottom == UITableViewCellContentView:0x7fc98362f940.bottom - 8 (active)>",
"<NSLayoutConstraint:0x600000fad680 'UIView-Encapsulated-Layout-Height' UITableViewCellContentView:0x7fc98362f940.height == 95 (active)>"
)
Will attempt to recover by breaking constraint
<NSLayoutConstraint:0x600000facd20 UIImageView:0x7fc98362fe80.height == 86.9 (active)>
我相信您必须降低纵横比约束的优先级以避免破坏约束消息。
而且,我认为你对 nn.5
价值观的看法不太正确。
例如...420:360
比率:
将视图的宽度设置为 59
会导致自动布局高度计算为
59.0 * 360.0 / 420.0 == 50.571428571428569
自动布局不会为您提供 50.571428571428569
的实际视图高度...它会将其四舍五入到最接近的 .5
即 50.5
少于 请求所以你得到错误。
与:
59.4 * 360.0 / 420.0 == 50.914285714285718
它四舍五入为 51
, 大于 请求,而您 没有 得到错误。
再举几个例子:
58.5 * 360.0 / 420.0 == 50.142857142857146 // rounds down -- error
58.4 * 360.0 / 420.0 == 50.057142857142857 // rounds down -- error
58.3 * 360.0 / 420.0 == 49.971428571428568 // rounds up -- NO ERROR
将宽高比约束更改为 Priority: 999
会得到相同的 sizing 结果(四舍五入到最接近的 .5
)但 not 抛出破坏约束错误。
由于您将获得相同的实际大小,因此更改优先级只会允许自动布局为 "do the right thing",并且不会对您的布局产生任何其他影响。
我正在尝试为 tableviewcell 中的图像设置纵横比,但由于 iOS 无法使用不同于 .5 或 . 0.
如果约束接近其值,或者它被四舍五入为擦除小数点的值,那么有什么办法可以使约束不 fail/break?
经过多次尝试,我知道问题是因为小数,我将通过以下示例向您展示:
用下面的代码
postImage.heightAnchor.constraint(equalToConstant: 88.9).isActive = true
我收到以下错误:
Will attempt to recover by breaking constraint
<NSLayoutConstraint:0x6000036067b0 UIImageView:0x7fb76fe397e0.height == 88.9 (active)>
而视图视图层级图像的大小是89
当我设置不同于 .5 或 .0 的高度时,它会四舍五入为最接近的值 .5 或 .0(如果我设置 88.6,它会四舍五入为 88.5 并抛出错误),但如果我设置任何以 .0 或 .5 结尾的值,它就可以正常工作!
(我知道我可以设置较低的优先级,但它会给我带来更多意想不到的行为)
(我正在以编程方式执行此操作,通过 Xib 执行此操作不是一个选项:))
编辑:整个日志:
(
"<NSLayoutConstraint:0x600000facd20 UIImageView:0x7fc98362fe80.height == 86.9 (active)>",
"<NSLayoutConstraint:0x600000facd70 V:|-(0)-[UIImageView:0x7fc98362fe80] (active, names: '|':UITableViewCellContentView:0x7fc98362f940 )>",
"<NSLayoutConstraint:0x600000face10 UIImageView:0x7fc98362fe80.bottom == UITableViewCellContentView:0x7fc98362f940.bottom - 8 (active)>",
"<NSLayoutConstraint:0x600000fad680 'UIView-Encapsulated-Layout-Height' UITableViewCellContentView:0x7fc98362f940.height == 95 (active)>"
)
Will attempt to recover by breaking constraint
<NSLayoutConstraint:0x600000facd20 UIImageView:0x7fc98362fe80.height == 86.9 (active)>
我相信您必须降低纵横比约束的优先级以避免破坏约束消息。
而且,我认为你对 nn.5
价值观的看法不太正确。
例如...420:360
比率:
将视图的宽度设置为 59
会导致自动布局高度计算为
59.0 * 360.0 / 420.0 == 50.571428571428569
自动布局不会为您提供 50.571428571428569
的实际视图高度...它会将其四舍五入到最接近的 .5
即 50.5
少于 请求所以你得到错误。
与:
59.4 * 360.0 / 420.0 == 50.914285714285718
它四舍五入为 51
, 大于 请求,而您 没有 得到错误。
再举几个例子:
58.5 * 360.0 / 420.0 == 50.142857142857146 // rounds down -- error
58.4 * 360.0 / 420.0 == 50.057142857142857 // rounds down -- error
58.3 * 360.0 / 420.0 == 49.971428571428568 // rounds up -- NO ERROR
将宽高比约束更改为 Priority: 999
会得到相同的 sizing 结果(四舍五入到最接近的 .5
)但 not 抛出破坏约束错误。
由于您将获得相同的实际大小,因此更改优先级只会允许自动布局为 "do the right thing",并且不会对您的布局产生任何其他影响。