An issue with Content Hugging and Content Compression Resistance, Autolayout Constraints

烈酒焚心 提交于 2019-12-01 12:12:57

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!