内容拥抱和内容压缩阻力,自动布局约束的问题 [英] An issue with Content Hugging and Content Compression Resistance, Autolayout Constraints

查看:196
本文介绍了内容拥抱和内容压缩阻力,自动布局约束的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已修改此问题,以提供更多资讯并加以澄清。



我想要一个动态表格视图单元格,具有灵活的UITextView高度和可选的UIImageView。基于UITextView的内容大小和可选的UIImageView,单元格可以缩小或扩展。



这是我期望的图片):




  • 正常尺寸:TextView的高度是固定的(例如77)。 ImageView的宽度和高度也是固定的(例如130,130)

  • 当TextView的内容大小递减时,单元格缩小,ImageView向上移动。

  • 当ImageView中没有图像时,ImageView不显示,单元格缩小。





下面是视图的结构:




  • mainView是contentView的子视图

  • topView,textView,bigImageView,bottomView是mainView的子视图





但是,我实际上得到的如下。




  • 左图:如果内容大小较大,则无法看到ImageView。

  • 右图:如果内容大小为一行,ImageView可以




  • 警告问题如下: b
    $ b

    >



    如果我通过选择添加缺失限制来修复它,我得到的结果如下:




    • 右侧图片显示了TextView的高度不再有弹性。





    TextView的约束,内容拥抱优先级,内容压缩阻力和内在大小:




    • 我没有为此TextView使用内在大小



    >



    ImageView的限制,内容拥抱优先级,内容压缩阻力和内部大小:




    • 使用此ImageView的内在大小





    我一直在使用TextView和ImageView的拥抱优先级和抗压优先级,但没有运气。没有结果是我的预期。



    请帮助指出设置中有什么问题,以及如何实现我的目标。

    解决方案

    这里棘手的部分似乎是,当有一个图像,你想它是130x130但是当没有图像,你想要隐藏。 / p>

    我的建议是为高度和宽度添加一个约束,它的值为<= 130而不是= 130,并为宽高比添加一个约束:1: 1。



    当没有图像时,这将允许图像视图缩小到0x0。



    是一个图像,它会缩放到图像大小,但保持正方形。它永远不会超过130x130。不是你想要的,但关闭。



    你不应该担心拥抱和压缩我不认为。



    想要我想需要代码。您需要为高度和宽度约束创建IBOutlets,但这次使它们= 130。在你的代码中,当你将图像设置为图像视图时,将代码中的约束常量设置为130或0.我认为是实现你所要求的唯一方法。要做到这一点你CTRL从你的宽度和高度约束拖动到控制器的.h,并创建IBOutlets。这是我如何处理需要基于细胞的数据模型的状态的决定的约束。


    I have modified the question to be more information provided and clear.

    I wanted to have a dynamic table view cell, with a flexible height of UITextView and an optional UIImageView. Based on the content size of the UITextView and the optional UIImageView, the cell can be shrunk or extended.

    Here is what I expected to have (with the below picture):

    • Normal size: height of TextView is fixed (say 77). ImageView width and height are also fixed (say 130, 130)
    • When content size of TextView decreasing, cell is shrunk and ImageView moves up.
    • When there is no image in ImageView, the ImageView is not displaying, cell is shrunk.

    The below is the structure of views:

    • mainView is subview of contentView
    • topView, textView, bigImageView, bottomView are subviews of mainView

    However, what I actually got is as follows.

    • Left picture: if the content size is large, then the ImageView cannot be seen.
    • Right picture: If the content size is one line, the ImageView can be seen, but its width and height are not fixed 130, 130

    The warning issue is as follows:

    If I fixed it by selecting "Add Missing Constraints", the result I got is as below:

    • Right picture showing that the height of TextView is not flexible anymore.

    TextView's constraints, Content Hugging Priority, Content Compression Resistance and Intrinsic Size:

    • I didn't use Intrinsic size for this TextView

    ImageView's constraints, Content Hugging Priority, Content Compression Resistance and Intrinsic Size:

    • I used Intrinsic size for this ImageView

    I have been playing around with the Hugging Priority and Compression Resistance Priority for both TextView and ImageView but no luck. None of the results are what I expected.

    Please help point out what is wrong in the settings and how to achieve my goal.

    解决方案

    The tricky part here would seem to be that when there is an image you want it to be 130x130 but when there is no image you want it to be hidden.

    My suggestion would be to add a constraint for the height and width which is <= 130 rather than = 130 and to add a constraint for the aspect ratio which is 1:1.

    This will allow the image view to shrink to 0x0 when there is no image.

    When there is an image it will scale to the image size, but stay square. It will never go larger than 130x130. Not quite what you wanted but close.

    You should not need to worry about hugging and compression I do not think.

    To do exactly what you want I think requires code. You need to create IBOutlets for the height and width constraints but this time make them =130. In your code when you set the image into the image view, set the constraint constants in code to either 130 or 0. This I think is the only way to achieve exactly what you are asking for. To do this you CTRL drag from your width and height constraints into the .h for the controller and create the IBOutlets. This is how I deal with constraints which require a decision based on the state of the data model for cells.

    这篇关于内容拥抱和内容压缩阻力,自动布局约束的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆