I have a simple tabbed application whose first tab is a UITableViewController
. After populating some data, I noticed that the top of the table view overlaps the status bar.
I've tried messing with the edgesForExtendedLayout
and similar settings, but have not found the magical combo. Does anyone know how to correct this?
Steps to reproduce:
UIViewController
and replace it with a UITableViewController
UITableView
with some dataHere's a couple screenshots of the setup and the issue:
Just modify the contentInset
property of your table view, which can add some padding around your content. In viewDidLoad()
add the following:
tableView.contentInset.top = 20
In Objective-C, you can't assign to the top directly, so do it like so:
self.tableView.contentInset = UIEdgeInsetsMake(20, 0, 0, 0);
Of course it can be any arbitrary value, in this case, it is the height of the status bar.
As suggested in comments, one way is to (in Swift 3):
override func viewDidLoad() {
super.viewDidLoad()
tableView.contentInset.top = UIApplication.shared.statusBarFrame.height
}
But that not as clean. It is better to embed the UITableViewController in a UINavigationController (Editor > Embed In > Navigation Controller) and uncheck "Shows Navigation Bar" in the inspector. No tweaking needed!
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With