Extra 20px Top Inset UIScrollView Issue

Here the accepted answer of Pauls in SWIFT 4:

override func viewDidLoad() {
    backGroundScrollView.contentInsetAdjustmentBehavior = .never
    backGroundScrollView.contentInset = UIEdgeInsetsMake(0, 0, 0, 0)

    super.viewDidLoad()
}

For iOS 11.1 and later:

Swift 5

if #available(iOS 11.1, *) {
    self.tableView.verticalScrollIndicatorInsets.top = CGFloat.leastNonzeroMagnitude
}

ObjC

if (@available(iOS 11.1, *)) {
    self.tableView.verticalScrollIndicatorInsets = UIEdgeInsetsMake(__DBL_MIN__, 0, 0, 0);
}

Reason why I'm not using 0, but CGFloat.leastNonzeroMagnitude is that in some cases iOS applies default value, when value is set to 0


Do you have your controller embedded in a navigationController? It seems that iOS is leaving 20px at the top for a status bar but your controller doesn´t have one.

Either way, in your viewDidLoad it should work if you adjust the scrollView before calling super:

   - (void)viewDidLoad {
    self.automaticallyAdjustsScrollViewInsets = NO;
    self.scrollView.contentInset = UIEdgeInsetsMake(0,0,0,0); 
    [super viewDidLoad];
    }

The reason why iOS might be automatically forcing your scrollView to have a 20px TOP contentInset is because it might be the view with index 0 in your main view. You can read further here: http://b2cloud.com.au/how-to-guides/uiviewcontroller-changes-in-ios7

By default, a UIViewController will automatically adjust the content insets of it’s UIScrollView (including UITableViews) at view index 0, the very back. This means if there’s a button, label, or any other view behind your table view, you wont get this behaviour for free.