34
votes

My root controller is a TabBarController (tabbed application). One of the tabs, is a UITableViewController. When I switch to that and scroll through the items, they show up under the status bar at the top (signal, battery, etc). I don't want that. I want that to be opaque or... something. It's visually jarring with the text of the table cells underlapping the status stuff.

Can I fix this from my Storyboard with some attributes setting that I don't understand? Or do I need to add some methods to my subclasses? Or maybe I need to wrap my UITableViewController with some other kind of controller?

I've tried numerous variations of the ViewController Layout and Extend Edges settings in the Storyboard attributes page, but none of them seem to change it for the better.

Update: I think my problem is very similar to iOS 7: UITableView shows under status bar. The only difference, is that I'm embedded in a TabBarController, and that case is as the root view. I tried the solution listed there of embedding in a NavigationController and setting Show Navigation Bar to False, but it didn't make any difference.

Screen Shots:

My storyboard (shrunk) showing a tabbed controller, with 2 children, one single view, and the other the table view.

Storyboard

Settings for the tab bar controller

tab bar controller settings

Settings for the table view controller

table view controller settings

What the app ends up looking like on my phone

enter image description here

How the Story Ended

Despite lots of answers below, none of them really worked. Some kind of a little, but not really. I tried the Embed in NavigationController approach as well, and that also had issues. What did work though, was this:

  1. Add UIViewController
  2. Set child controller relationship with it and tab bar controller (just like the other two I already had)
  3. Add a TableView (not controller) to the new UIViewController, position as desired, it'll snap to the bottom of the status bar
  4. Set the TableView's delegate and tableSource as the new controller
  5. Create a custom UIViewController subclass and update the class type of the controller in the storyboard
  6. Copy the table related methods from my custom UITableViewController subclass to my new subclass
  7. Select my prototype table cell from the original, and command+drag it to the new table view
  8. Happily delete the original TableViewController (and wrapper NavigationController) too
  9. Update the tab bar item to match the previous
  10. Chock another one up for "you're trying to hard"
13
have you tried in storyboard: attributes inspector -> extend edges -> uncheck under top bars. ?Ilario
@Ilario: yes. makes no difference :(Travis Griggs
@Ilario that worked for me +1ingh.am

13 Answers

33
votes

Try this in viewDidLoad:

self.tableView.contentInset = UIEdgeInsetsMake(20, 0, 0, 0);

20 px being the height of the status bar. If you have a navigation bar use 64 instead of 20

15
votes

In case anyone is still reading this thread:

What worked for me is to also uncheck the "Extend Edges" options in the parent tab bar controller. That gives a consistent behaviour in iOS7 as well as iOS6.

6
votes

I was having the same problem when using the SWRevealController.

Using the comments above and below I was able to get it to work by putting this in the

-(void)viewWillAppear instead of ViewDidLoad
     self.tableView.contentInset = UIEdgeInsetsMake(20, 0, 0, 0);
4
votes

Have you tried adding something like this to the view controller's viewWillAppear method:

 if ([self respondsToSelector:@selector(setEdgesForExtendedLayout:)])
 {
    self.edgesForExtendedLayout = UIRectEdgeNone;
 }
2
votes

In case anyone misses the How the story ended section at the end of the (now long) question, the short answer is: Use a simple UIViewController with a TableView, instead of a TableViewController if you want to achieve the stated goal.

2
votes

I added the following to viewWillAppear

self.tableView.contentInset = UIEdgeInsetsMake(22, 0, 0, 0);
2
votes

You can also solve this through Storyboard.

Select the Table View from the Project Outline (left side of the editor) and then go to Properties (right side) > Size inspector tab > Scroll View > Content Insets > Top

2
votes

suppose your table's @IBOutlet is

 @IBOutlet var tableView: UITableView!

create a function prepareTable(). add the below line into that method

tableView.contentInset.top = UIApplication.shared.statusBarFrame.height

call this method from ViewDidLoad()

1
votes

Looks like you just want to make the NavBar nonTranslucent, you could try using

[self.navigationController.navigationBar setTranslucent:NO];
1
votes

I encountered the same problem and the solution that worked for me was to add a section header view:

-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section

I then added 20 to the origin.y of my text on the header.

I then changed the header height by adding 20 to the original height using the

-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return myHeight+20;
}
1
votes

For me an easy solution was (code is in C# that's because appears to be invalid)...

  1. Just in the UITableViewController constructor create a new UIView (set same dimension of the TableView).

  2. Add the TableView to the new created view.

  3. Set new created View to the UITableViewController's View property...

On the constructor of the UITableViewController

var view = new UIView ();
view.Frame = this.TableView.Frame;
view.AddSubview (this.TableView);

this.View = view;
1
votes

As of iOS 8.4 I didn't get anywhere with the various storyboard options listed in other answers.

I worked around this without resorting to any dimensional constants by putting a regular view in my tab controller, then filling that with a "Container View" which I then connected to my UITableViewController with an "embed" segue.

My table now respects both my navigation bar and the tab bar at the bottom.

0
votes

Let's say your Outline view, or Storyboard, is a Tab Bar Controller that currently has a Table View Controller. When you run the app, your table view cells are running under the status bar that seems to have a transparent background. This sucks!

If you spent hours trying everything you could find on StackOverflow to no avail, felt like maybe it really was time to consider a new career and were preparing to dial 1-800-LUV-TRUK or 1-800-SHT-KIKR, give yourself a pat on the back, open up whatever elixir you drink in times such as this, because it's not your fault. Really. Travis is absolutely right that no amount of code in viewDidLoad, viewWillAppear, or viewDidAppear or button selecting/deselecting in IB will help in this situation.

Travis' solution above will certainly work, but it's a bit long, there's copying and pasting of code, I have a short attention span such that an episode of Bugs Bunny feels like a full-length movie to me, so I just know that I'll screw-up anything that complicated. But, hey, your mileage may, nay likely will, vary. Anyhoo...

In Xcode 7.3 for an app running iOS 9 (I assume 7 and 8 but I don't know this for certain and am currently too lazy to check) there is an easier way that doesn't require one to write any code. And it's done all within Xcode's Interface Builder.

(Caveat: Sorry if any of the terms aren't accurate. Please let me know where I was mistaken and I'll correct any mislabeling.)

  1. Go to the Utilities area of Interface Builder and select the Object library from the library pane.

  2. Select a Navigation View Controller object and drag it into your Storyboard scene. You'll notice that two scene items appear while you're dragging–these are a Navigation Controller Scene and a Table View Controller Scene.

  3. Delete the duplicate Table View Controller Scene that came-along with your Navigation Controller Scene.

  4. Select the relationship connection between your Tab Bar Controller and your Table View Controller and hit "Delete".

  5. Reposition the Navigation Controller and your Table View Controller the way you want in your storyboard.

  6. Control-drag from your Tab Bar Controller Scene to the Navigation Controller Scene and select "Relationship Segue, view controller".

  7. Control-drag from your Navigation Controller Scene to your Table View Controller Scene and select "Relationship Segue, root view controller".

  8. Lastly, in the Utilities' Objects library, drag a Navigation Item object into your Table View Controller Scene's Table View.

Now when you run your app, you will have a navigation bar and your table view cells will no longer appear under a transparent status bar.

Hope this helps.