向UITableViewCell添加约束 [英] Adding Constraints to UITableViewCell
问题描述
我在获取约束以无法在 UITableViewCell
上工作时遇到麻烦.该单元格当前有2个文本字段和一个带有图像的按钮.
iPhone X故事板
当前没有任何约束,无论我在哪台设备上进行测试,单元格的内容都不会改变位置.例如在iPad上看起来像这样
iPad故事板
当我添加约束时,它似乎可以正常工作,但是当我运行该应用程序时,内容全部搞砸了.
以下是带有约束条件的屏幕快照,以及我正在使用的约束条件.
受约束的iPhone X模拟器
谁能告诉我在所有设备上使它看起来像这样的特定限制条件?
不受限制的iPhone X模拟器
我在应用程序的其余部分没有遇到约束问题,但是由于某些原因,单元格中的自动布局约束正在逃避我的理解.
谢谢!
更新
我能够用以下问题的答案解决问题:
I am having trouble getting constraints to work on my UITableViewCell
. The cell currently has 2 text fields and a button with an image.
iPhone X Storyboard
Currently without constraints the cell content doesn't change position no matter which device I test it on. For instance on an iPad it looks like this
iPad storyboard
When I add constraints it looks like it is going to work, but then when I run the app the content is all screwed up.
Here are screenshots of what it looks like with constraints, and what constraints I am using.
iPhone X Simulator with constraints
Can anyone tell me which specific constraints I need to make it look like this on all devices?
iPhone X Simulator without constraints
I haven't had issues with constraints with the rest of the app, but for some reason Autolayout constraints in cells are evading my understanding.
Thank you!
UPDATE
I was able to fix the issue with the answers from this question Autolayout is ignored in Custom UITableViewCell
After that the constraints @leedex suggested worked wonderfully.
Your noteTextLabel has no leading or trailing constraints. The leading constraint should be to the superView and the trailing constraint should be to the left side of the button.
Your flagButton should have top and trailing constraints, where both are to the superView. Or the button could have a centerY constraint and a trailing constraint instead, where the centerY constraint is the same as the superView's and the trailing constraint is to the superView. The button should also have width and height constraints.
Your dateLabel should have leading, top, bottom and trailing constraints, where the leading is to the superView, top is to the noteTextLabel, the bottom is to the superView, and the trailing is to the flagButton.
Here is an example screenshot, notice how trailing, top, leading, and bottom constrains are visible:
这篇关于向UITableViewCell添加约束的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!