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

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

问题描述

我已经修改为更多信息提供的问题和明确的。
结果

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

我想有一个动态表视图细胞,用的UITextView的高度灵活和一个可选的UIImageView。基于所述的UITextView和可选的UIImageView的内容的大小,小区可以收缩或扩展

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.

这是我期望有(与下图):


  • 普通大小:TextView的高度是固定的(比如77)。 ImageView的宽度和高度也固定(比如130,130)

  • 在当前的TextView的内容大小减少,细胞收缩和ImageView的向上移动。

  • 当在ImageView的任何形象,ImageView的不显示,细胞收缩。

以下是意见的结构为:


  • MAINVIEW是内容查看子视图

  • 冠捷,TextView的,bigImageView,bottomView是MAINVIEW子视图

然而,事实上,我是如下:


  • 左图:如果内容尺寸大,则ImageView的不可见

  • 右键图像:如果内容的大小是一条线,所述ImageView的可以看出,但其宽度和高度是不固定的130,130

警告的问题如下:

如果我选择添加缺少约束,我得到的结果如下固定它:


  • 表示的TextView的高度不灵活了右图。

的TextView的限制,内容拥抱优先,内容的COM pression电阻和固有尺寸:


  • 我没有使用固有大小此的TextView

的ImageView的限制,内容拥抱优先,内容的COM pression电阻和固有尺寸:


  • 我用本质尺寸本的ImageView

我已经与抱死优先,并且通讯pression性优先为的TextView和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.

推荐答案

在这里最棘手的部分似乎是,当有一个形象,你希望它是130x130,但是当没有像你希望它被隐藏。

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.

我的建议是增加一个约束的高度和宽度为< = 130,而不是= 130和增加对纵横比为1约束:1

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.

这将使图像视图缩小为0x0的时候有没有图像。

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

当有将扩展到图像大小,但保持方形的图像。它永远不会比130x130大。不太你想要什么,但接近。

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.

您应该不需要担心拥抱和COM pression我不认为。

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

要做到正是你想要我认为需要code。您需要创建的高度和宽度的限制IBOutlets但这次让他们= 130。在您的code当图像设置为图像视图,设置code约束常量要么130或0。这个我觉得是实现你所要求正是对的唯一途径。要做到从宽度和高度的限制这一点,你Ctrl拖动到.H控制器和创建IBOutlets。这就是我如何处理这需要基于单元的数据模型的状态决定的约束。

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.

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

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