7

I'm having trouble with the contentInset property. I have a UITableView, with dynamic cell sizes (AutoLayout). I'm setting the contentInset property to leave some space above the top of the content. But I'm getting the following result:

Unexpected Result

The content is in blue, the content inset in purple. When the table view first appears, it is in the left situation. I can scroll to get to the right situation, that is working, but I would like the table view to appears directly as in the right illustration — which I thought would be the default behavior.

How can I achieve that?

PinkeshGjr
  • 8,460
  • 5
  • 41
  • 56
Morpheus
  • 1,189
  • 2
  • 11
  • 33

4 Answers4

10

Not sure if it's the best way but I fixed it by adding:

tableView.contentOffset.y = -70

after the line:

tableView.contentInset = UIEdgeInsets(top: 70, left: 0, bottom: 50, right: 0)
Morpheus
  • 1,189
  • 2
  • 11
  • 33
1

you can scroll programmatically when the view loads.

tableView.setContentOffset(CGPoint(x: 0, y: -70), animated: false)
  • That's what I ended up doing, not very pretty though. I thought the scrollview would automatically adjust so that it's scrolled "to the top" directly – Morpheus Jan 17 '19 at 14:54
0

This is how it can be fixed easily from the Storyboard:

Table View > Size Inspector > Content Insets: Never

Gareth Miller
  • 332
  • 1
  • 3
  • 10
  • I'm not using Storyboard, but I tried using scrollView.contentInsetAdjustmentBehavior, it didn't help – Morpheus Jan 17 '19 at 12:51
0

You provide an UIEdgeInset object,

UIEdgeInsets(top: 50, left: 0, bottom: 0, right: 0)

The top property shows the distance from top of content to top border of the area.

Talha Ahmad Khan
  • 3,416
  • 5
  • 23
  • 38