Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
282 views
in Technique[技术] by (71.8m points)

ios - Autolayout is ignored in Custom UITableViewCell

Despite having set constraints to all elements, including the vertical ones needed for the cell to calculate its height, auto-layout seems to be ignored: all cells are squeezed.

Here's a screenshot of the result and of the constraints in the storyboard. enter image description here

enter image description here

In the VC that holds the tableView, here's the code in viewDidLoad:

tableView.estimatedRowHeight = 120.0
tableView.rowHeight = UITableViewAutomaticDimension

Commenting out the second line gives cells with a height of 120.0 but Autolayout is ignored as well.

Thanks


Update

To simplify the interface, I've left a single label with, as constraints:

  • Leading space to superview
  • Top space to superview
  • Fixed width and height (100 & 100)
  • Bottom space to container margin to make sure that the cell has all vertical constraints to determine its height

And with this simplified interface, auto-layout is still not taken into account, which hints me that the problem did not come from badly set constraints.

In the Size Inspector, the row height is set on 120 and Custom is checked. The cell has the right custom class, the cell reuse identifier is correct.


Update: solution found

This was a simple mistake, see my answer below.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Auto-layout was ignored because both the prototype cell AND the UIView of the cell had been given the custom cell class in IB.

Setting the UIView back to UIView class solved the problem.

just to be über clear


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

56.9k users

...