Search code examples
swiftuitableviewtableviewstatusbar

Stop tableView scrolling under status bar (Swift 5, Xcode 15)


I have a tableView inside -> navigationViewController -> tabBarController.

enter image description here

The tableView keeps scrolling under the statusBar no matter what I do. I like it to stop underneath the status bar.

All viewControllers are configured the standard way. I configured them in many different ways and tried many combinations.

enter image description here

No matter what I do the tableView keeps scrolling under the status bar.

enter image description here

I didn't felt the need to configure that in code, but I just adjusting contentInset's and scrollIndicatorInset's without success.

tableView.scrollIndicatorInsets = UIEdgeInsets(top: 20.0, left: 0.0, bottom: 0.0, right: 0.0)
tableView.contentInset = UIEdgeInsets(top: 20.0, left: 0.0, bottom: 0.0, right: 0.0)

The adjustment just pushes the tableView another 20px down.

enter image description here

This is the size of the tableView at the moment. I did the background orange for clarity.

enter image description here


Solution

  • You cannot "have the status bar opaque". It is transparent. You can put something behind it if you want to see that color through it.

    The problem with the scrolling, however, is merely that your autolayout constraints are wrong. The table view scrolls behind the status bar because the table view is behind the status bar. Because you put it there. To stop the table view from being behind the status bar, just pin its top to the safe area top with a constant of 0. The safe area top is the status bar bottom (in this situation).

    enter image description here