简体   繁体   中英

UITableViewCell Constraints

So I've been trying to make a custom layout of a UITableViewCell that it contains an image and label, as you can see it in the picture below.

自定义布局

The main problem is the compression and hugging I haven't put my finger on it and I can't understand why the image expands, I have changed the compression and hugging of both

IMAGE SETTINGS:

图片

LABEL SETTINGS:

标签

Any Suggestions on how I can fix the shrinking of the label?

您可以给ImageView设置约束,例如Leading,Top,Height(Itself),Width(Itself)和尾随空间以进行标签,然后再设置标签即Top,Trailing和Height本身。希望它可以根据您的要求工作。

Content hugging is scaled so that larger numbers mean "do more hugging". In other words, your label (Content hugging priority = 251) will try to make its size match the content size more than your image (Content hugging priority = 249).

To do what you want, you need the priorities the other way around.

That said, you'll probably find that the layout is better anyway if you set the image to be a fixed width or fixed aspect ratio, otherwise it's going to look weird when these are in a table. If you do that, then you won't need to adjust the hugging priority because everything will be constrained.

Also, @matthews is right, you should get rid of the Center Y constraint because it's meaningless when it's together with the Top and Bottom constraints.

Hope this may help you:

For UIImageView 在此处输入图片说明

For UILabel 在此处输入图片说明

Use below code for self sizing cell

tableView.estimatedRowHeight = 100.0
tableView.rowHeight = UITableViewAutomaticDimension

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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