自定义自动布局的UITableViewCell [英] Auto layout with custom UITableViewCell
问题描述
我如何得到一个自定义的UITableViewCell的内容转移并使用自动布局调整?
How do I get the contents of a custom UITableViewCell to shift and resize using Auto Layout?
为了使问题更清楚,我已经指派内容查看
我的自定义单元格中有一个浅灰色的背景色。为了减少这种可能的最小的问题,我的自定义单元格只有一个的UIImageView
名为 ratingImageView
显示黄色一定数量星星;三个这样的细胞可以看到下面的图片
To make the issue more clear, I've assigned the contentView
of my custom cell to have a light gray background color. To reduce this to the smallest problem possible, my custom cell has only one UIImageView
named ratingImageView
that shows some number of yellow stars; three such cells can be seen in the image below.
我的 ratingImageView
只有4个的限制; (1)宽度= 81,(2)高度= 40,(3)居中对齐Y以上海华,(4)尾随空间的SuperView = 20。我本来期望尾随的空间,上海华约束图像视图强制向左时,标准的删除按钮,滑动删除出现。它没有,但是,由于可以在下面的图像中看到。
My ratingImageView
has only 4 constraints; (1) width =81, (2) height =40, (3) align center Y to superview, and (4) trailing space to superview =20. I would have expected the trailing space to superview constraint to force the image view to the left when the standard delete button appears on swipe to delete. It does not, however, as can be seen in the image below.
我试过限制的许多不同的组合,并且不能作出我期望 ratingImageView
转变。
I've tried many different combinations of constraints and cannot make the ratingImageView
shift as I would expect.
我通过一个良好的入门教程故事板工作遇到了这个问题>。我没有问题,用好老支柱和弹簧,使这个工作,但我决定与自动布局来尝试一下,有没有运气。
I encountered this problem by working through a good introductory Storyboard tutorial. I had no problem making this work using good old struts and springs, but I decided to try it with Auto Layout and have had no luck.
推荐答案
我有同样的问题,当我转身自动布局 ON
为教程。
I had the same issue when I turned Autolayout ON
for the tutorial.
问题是因为约束横向空间(20)
。这种约束的优先级默认情况下,这意味着高优先级设置为 1000
。所以ImageView的是服从约束,使自己停留在它的位置不对应的UITableViewCell的(PlayerCell在这种情况下),当删除
显示按钮内容视图搬迁。
The issue is because of constraint Horizontal Space (20)
. The priority of this constraint is set to 1000
by default which mean high priority. So the imageView is obeying the constraint and making itself stay in its position without relocating corresponding to UITableViewCell's (PlayerCell in this case) content view when delete
button is displayed.
在我的同事合作,我们找到了这个问题的解决方案。添加约束的ratingImageView编程方式解决了这个issue.If任何人有比这更好的解决办法,请张贴在这里。
In Collaboration with my colleague we found the solution for this problem. Adding the constraints to the ratingImageView programmatically solved this issue.If anyone has a better solution than this please post here.
- 打开你的故事板,并选择PlayerCell并扩大其限制。
- 选择约束
横向空间(20)
,并在属性检查器中的优先级设置为任意值较低。 -
在PlayersViewController.m文件中添加以下code在
的cellForRowAtIndexPath
的tableView的委托方法:
- Open your story board and select the PlayerCell and expand its constraints.
- Select the constraint "
Horizontal Space (20)
" and in the Attributes inspector set its priority to any lower value. In PlayersViewController.m file add the following code in
cellForRowAtIndexPath
tableView's delegate method:
的UIImageView * ratingImageView = cell.ratingImageView;结果
*的NSDictionary字典= NSDictionaryOfVariableBindings(ratingImageView);
[cell.contentView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@H:[ratingImageView] |选项:0的指标:无意见:快译通];
这篇关于自定义自动布局的UITableViewCell的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!