UITableView scroll not working properly when inset content

scrollview contentinset not working
uitableview contentinset left right
tableview scrolling problem in ios
uitableview scroll to top

I want to add some inset to tableView to padding all your content. When im using only Top/Bottom content, all works fine. But trying to put left and right values, the tableView scroll dont work (see gif). What is wrong?

     @IBOutlet weak var tableView: UITableView!

override func viewDidLoad() {
    super.viewDidLoad()
    self.tableView.delegate = self
    self.tableView.dataSource = self
    self.tableView.clipsToBounds = true
    let insets = UIEdgeInsets(top: 20, left: 20, bottom: 20, right: 20)
    self.tableView.contentInset = insets
}

i suggest you simply to separate top-bottom and left-right insets: you can add top-bottom insets to the table view and left-right insets to your cells as constraints to content view.

This way scroll will work as you want.

how to fix tableview scrolling? · Issue #22 · bryankeller , tableView scrollToRowAtIndexPath:indexPath atS. but in other cases(i mean scrolling) contentInset is not a problem? do u dynamically recalc  Hello. I use your code wonderfully. And this time I make a app with Instagram SDK. So make a tableView and custom Header Section. tableView issued is solved, but problem is Header Section. When navigationBar hidden, Header Section is not

I think, you have wrong understanding how UIScrollView's contentInset works in scope of UITableView. Documentation says next about UIScrollView's contentInset:

Use this property to extend the space between your content and the edges of the content view. The unit of size is points. The default value is UIEdgeInsetsZero. By default, UIKit automatically adjusts the content inset to account for overlapping bars. You use this property to extend that distance even further, perhaps to accommodate your own custom content. Get the total adjustment—that is, the safe area plus your custom insets—using the adjustedContentInset property. To change how the safe area is applied, modify the contentInsetAdjustmentBehavior property.

So, setting contentInset do not actually modify contentSize. For instance, pay your attention how vertical content insets are changed when you install your view controller inside UINavigationController. By default, UIScrollView automatically modify contentInset to move content downwards in order to UINavigationBar does not overlap your content.

Starting from iOS 11 there is even more advanced APIs to work with safe areas, please find it here.

As for your problem, you need to modify UIScrollView's contentSize somehow in order to achieve desired behaviour. UITableView does not provide such API and table view's contentSize equals table view bound's size.

From my perspective there are three possible options:

  1. You could change your UITableViewCell view's hierarchy by adding another contentView with transparent background on top of UITableViewCell. Then make contentView's size smaller than superview's size and finally place all other UI components on top of content view.
  2. You could switch from UITableView to UICollectionView and use custom layout (you could use UICollectionViewFlowLayout). You can easily make it look like UITableView layout and set different insets for items and sections and even calculate custom contentSize.
  3. You can play with layout cycle, override layout cycle methods and set new contentSize to your UITableView, but I would not recommend to do that. Final solution could not be obvious and make code base messy.

Hope this helps!

UITableview: The Essential Solution for Scrolling iOS Interfaces, In a scroll view, that's not as easy to get right as it is in a regular UIView. For example, to display content with a vast area that you can zoom or scroll The system also configures the insets of any UITableView to keep cells  The line of code is working as it should. Explaination of contentOffset of a UITableView: For example, if you have a table view of height 100. Its content may be more than it’s view, say 150. contentOffset will tell the table from where in the content to start from. Say contentOffset = (0, 40), the content will be shown after 40 of it’s height.

A possible easy solution could be to add a padding using constraints in storyboard:

The automaticallyAdjustsScrollViewInsets Rabbit Hole, The gist of it is that Apple wants our scrollable content to go underneath stuff (​home of your view controller (such as with a UITableViewController ) or the subview at This means that we also fix the problem by reversing the order or our views I tested it with a scroll view as the root view as well, and it was properly inset. I have searched a lot for a solution but all the results have not quite been a solution to what I am trying to achieve. They all suggest scrolling to the last position of a table with scrollToRowAtIndexPath and populating the data in reverse. But this doesn't work if the table content is dynamic and in some instances not all the cells have data.

How to change the scroll indicator inset for a UIScrollView, Knowledge Base SwiftUI by Example Swift in Sixty Seconds Hacking with Swift It's common to adjust content insets of a scroll view or any class that to change the scroll indicator inset: the visual indicator bar on the right that can painlessly implement subscriptions for your app in hours, not months. Questions: I have an app where the UITableView‘s separator inset is set to custom values – Right 0, Left 0. This works perfectly in iOS 7.x, however in iOS 8.0 I see that the separator inset is set to the default of 15 on the right.

TableView w. Dynamic Height scrolls when reload |Apple , My UITableView with dynamic cell heights randomly scrolls when inserting new cells at the top. When doing that, I've not found a proper implementation that cells the contentSize can't be trusted for this purpose and was not working for me. section: 0), at: .top, animated: false); //Add offset; tableView. When UITableView is scrolled, the section header 's contentInset not being changed while scrolling section-headers-in-uitableview-when-inset-of

Reloading UITableView while Animating Scroll in iOS 11, Calling reloadData on UITableView may not be the most efficient way to update your cells, but sometimes it's Example of scroll animation getting interrupted by a `reloadData` call contentInset.bottom guard fabs(tableView. The final thing to watch out for is if you are updating cells individually as well, make sure your  So instead of 120, they send 30 (or instead of -120 when scrolling back, they send -30). This should result in scrolling at the same speed, just more smoothly. Some applications may not handle this correctly. For example, they may ignore partial scroll events, or they may interpret a partial (30) event as a full line (120).

Comments
  • Check constraint properly. and unselect Bounce Horizontally.
  • This is not a solution because the scrollable are it will be only the centered tableView. In my cause all the view, included the area with padding, has to be scrollable.