Mark Green Mark Green - 7 months ago 37
iOS Question

In iOS 9, if using a class other than UIViewController for a page, how can I create a constraint to avoid clobbering the status and/or tab bars?

All the guidelines suggest adjusting the Top Layout Guide to avoid clobbering the status bar. But, if the page is created with a View Controller other than UIViewController (for example, if it is created with UITableViewController because the page is mainly a table view) then it has no layout guides. How can I then avoid the status bar?


I've found the UITableViewController to be more trouble than it's worth, like this guy: How do I make my iOS7 UITableViewController NOT appear under the top status bar?

Now when I implement a table view I find it easier to make the TableView a property of the UIViewController, then you set the delegate and the datasource responsibilities to the UIViewController. From there you can customize it however you want really. You can play with the Storyboard options like the Status Bar setting (Inferred, None, Black etc) but in my experience I have found it works best by just putting a UITableView inside a UIViewController.

Example Header:

@interface MyViewController : UIViewController <UITableViewDelegate, UITableViewDataSource>

@property (weak, nonatomic) IBOutlet UITableView *myTableView;

Example Code in Controller

@synthesize myTableView;
//**** Table View Delegate and Data Source ****//

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
    static NSString *myCellIdentifier = @"myCell";
    MyTableViewCell *cell = (MyTableViewCell *)[myTableView dequeueReusableCellWithIdentifier:myCellIdentifier];
    //Customize the cell
    return cell;

-(NSInteger)tableView:(UITableView*)tableView numberOfRowsInSection:(NSInteger)section
    return [myDataSource count];

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
    //perform some action on click

Another example of UITableViewController without a NavigationController and problems with the status bar: iOS 7: UITableView shows under status bar