Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ios UITableViewController overlapping status bar [duplicate]

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:

  1. Create a new tabbed application
  2. Remove the first tab UIViewController and replace it with a UITableViewController
  3. Populate the UITableView with some data
  4. Run the application

Here's a couple screenshots of the setup and the issue: Xcode StoryboardUITableView overlapping status bar

like image 855
Albert Bori Avatar asked May 15 '15 20:05

Albert Bori


2 Answers

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.

like image 67
József Vesza Avatar answered Nov 12 '22 07:11

József Vesza


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!

like image 30
sanmai Avatar answered Nov 12 '22 07:11

sanmai