How to maintain the same margins in table view cells on different devices?

I am trying to develop an information based app with auto layout. Most of the pages are UITableViews, built in IB with static cells containing a simple text field. I would like the width of the text fields to automatically expand and the height to reduce if the device is rotated into landscape more, or if initially opened on a larger device.


Using autolayout, I am seeing in most cases that the width does automatically expand to fill the horizontal space. However, the height does not seem to reduce, which leaves in some cases a lot of space - a large margin - beneath the text. First question, how do I avoid this?


In some cases, the width does NOT expand across the screen, but an arbitrary margin seems to be set. This is sometimes applied to the entire text field within a table view cell, but in one case, the top two paragraphs extend across the screen, but the bottom paragraphs have a much larger right margin - which looks terrible.


I am not sure how to add images here, so put them here: https://stackoverflow.com/questions/58410325/how-to-control-the-right-margin-of-my-uitextfield

Replies

You were right not to post images in the message. They do not go through !


To achieve what you want, you should use the heightForRowAt delegate function

func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
   return UITableView.automaticDimension
}

See: h ttps://www.tutorialspoint.com/dynamically-change-tableview-cell-height-in-swift


Can also have a look at this long tutorial:

h ttps://www.raywenderlich.com/8549-self-sizing-table-view-cells

It is a good tutorial, but I have done it previously, and reviewed it for this problem. The problem occurs despite .automaticDimensions in the heightForRowAt function.


I often see an error that the constraints suggest a height of zero for table view cell content view. The only solution I found for this is to delete the constraints and reapply them, which sometimes removes the error, but often results in the text view not appearing. If I then add a height constraint, the view appears, but the height is now fixed for a single device.

but the height is now fixed for a single device.


Could you adapt the func to return the height you want depending on device height ?

For every device? In every orientation? Problem would be - if I change the contents, I have to change the height.


If I start from a new table view cell, add a new text view, and re-type the text (can't just paste it), then some of the issues improve - This was the only way I found to get rid of the large right hand margin issue.

I checked again in h ttps://www.raywenderlich.com/8549-self-sizing-table-view-cells.

- set the constraints in cell

- automaticHeight should be set in viewDidload.

tableView.rowHeight = UITableView.automaticDimension
tableView.estimatedRowHeight = 600          // May be different in your case


Could also do it in IB.

No need for heightForRow.

I've already got that and .automaticDimension in heightForRow.