UITableView ContentInset and ContentOffset
Asked Answered
K

4

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?

Kernan answered 17/1, 2019 at 11:50 Comment(2)
Does this help? #22021443Dissimilitude
@RakeshaShastri I saw that already and tried but it didn't workKernan
K
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)
Kernan answered 17/1, 2019 at 12:53 Comment(0)
M
1

you can scroll programmatically when the view loads.

tableView.setContentOffset(CGPoint(x: 0, y: -70), animated: false)
Menke answered 17/1, 2019 at 13:28 Comment(1)
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" directlyKernan
P
0

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

Table View > Size Inspector > Content Insets: Never

Periwinkle answered 17/1, 2019 at 12:34 Comment(1)
I'm not using Storyboard, but I tried using scrollView.contentInsetAdjustmentBehavior, it didn't helpKernan
W
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.

Willodeanwilloughby answered 17/1, 2019 at 12:42 Comment(1)
yeah this is what I'm doingKernan

© 2022 - 2024 — McMap. All rights reserved.