简体   繁体   English

iOS以编程方式在UITableviewcell中打破了宽高比约束

[英]iOS programmatically aspect ratio constraint gets broken in UITableviewcell

I am trying to set an aspect ratio to an image inside a tableviewcell and it is getting broken because iOS can't accomplish accomplish constraints for width or height with decimals that are different from .5 or .0. 我正在尝试为tableviewcell内的图像设置纵横比,但由于iOS无法完成宽度或高度的小数位数不同于.5或.0的完成约束,因此它已损坏。

There is anyway to make that a constraint doesn't fail/break if it is close to its value, or it gets rounded to a value erasing the decimal? 无论如何,如果约束接近其值,或者舍入为舍去小数的值,该约束不会失败/破坏?

I know that the problem is because of the decimal after many tries, I will show you with the following example: 我知道问题出在很多次尝试之后,因为小数,我将通过以下示例向您展示:

With the following code 用下面的代码

postImage.heightAnchor.constraint(equalToConstant: 88.9).isActive = true

I get the following error: 我收到以下错误:

    Will attempt to recover by breaking constraint 
<NSLayoutConstraint:0x6000036067b0 UIImageView:0x7fb76fe397e0.height == 88.9   (active)>

And the view view hierarchy the size of the image is 89 并且视图视图层次结构的图像大小为89

在此处输入图片说明

It happens to me when I set a height that is different from .5 or .0 it gets rounded to the nearest value .5 or .0 (If I set 88.6 it gets rounded to 88.5 throwing the error), but If I set any value that ends in .0 or .5 it works smooth! 当我将高度设置为.5或.0时,它会四舍五入到最接近的值.5或.0(如果我将其设置为88.6,它将四舍五入为88.5,则抛出错误),但是如果我将其设置为以.0或.5结尾的值可以正常工作!

(I know I can do it setting a lower priority but it can give me more unexpected behaviors) (我知道我可以将其设置为较低的优先级,但是它可以给我带来更多的意外行为)

(I am doing it programmatically, doing it through a Xib is not an option :) ) (我正在以编程方式进行操作,通过Xib进行操作不是一种选择:))

EDIT: Whole log: 编辑:整个日志:

    (
        "<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)>

I believe you must reduce the priority on your aspect-ratio constraint to avoid the broken constraint messages. 我相信您必须降低长宽比约束的优先级,以避免破坏约束消息。

And, I think you're not-quite-correct about nn.5 values working. 而且,我认为您对nn.5值的工作并不完全正确。

For example... With a 420:360 ratio: 例如...比率为420:360

setting the width of the view to 59 results in an auto-layout height calculation of 将视图的宽度设置为59导致自动布局高度计算

59.0 * 360.0 / 420.0 == 50.571428571428569

Auto-layout is not going to give you an actual view height of 50.571428571428569 ... it's going to round it to the nearest .5 which is 50.5 and that is less than requested so you get the error. 自动布局不会为您提供实际的视图高度50.571428571428569 ...,而是50.571428571428569其四舍五入至最接近的.5 ,即50.5小于要求的高度,因此您会得到错误。

With: 附:

59.4 * 360.0 / 420.0 == 50.914285714285718

it rounds to 51 , which is greater than requested, and you do not get the error. 它四舍五入到51 ,这请求的值大,并且您没有得到错误。

A few more examples: 还有更多示例:

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

Changing your aspect-ratio constraint to Priority: 999 gives the same sizing results (rounds to the nearest .5 ) but does not throw the broken constraint error. 改变你的纵横比约束Priority: 999给出了同样的施胶效果(四舍五入为最接近的.5 ),但不会引发破约束错误。

Since you will get the same actual size, changing the priority will simply allow auto-layout to "do the right thing" and shouldn't have any other effect on your layout. 由于您将获得相同的实际大小,因此更改优先级将仅允许自动布局“执行正确的操作”,并且不会对布局产生任何其他影响。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM