Status Bar and content placement


#1

In iOS 7.x, is the only way to prevent content from being obscured (and non-interactive) by the status bar to manually locate it away from the status bar’s location? When I originally created my header view for the chapter assignment, I made the height the same height as the buttons and when I ran the application, the buttons were placed underneath the status bar and the only way I could get them to be interactive was to make the header view taller and move the buttons to the bottom of the view on the XIB file. Is there no setting to have content begin placement at the bottom of the status bar automatically? Would you need to specify the view location with CGRectMake?


#2

You can manually add some padding to the top of the header view to accomplish this. Here is a potentially cleaner approach:

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    self.tableView.contentInset = UIEdgeInsetsMake(20, 0, 0, 0);
    self.tableView.scrollIndicatorInsets = UIEdgeInsetsMake(20, 0, 0, 0);
}

The contentInset line pushes the start of the content down 20 points without adjusting the frame of the table view. The scrollIndicatorInsets pushes the start of the scroll indicators down 20 points so they start just under the status bar.

View controllers have a property called automaticallyAdjustsScrollViewInsets. When a table view controller is inside a navigation controller, as we do later in the book, this property essentially does the code above, but has 64 instead of 20 (20 is just the status bar height, 64 is status bar height + navigation bar height).

Why this property doesn’t adjust the scroll view insets to account for the status bar when not in a navigation controller is perplexing to me.

Note: You’d not want to have this code in your view controller when it is inside a navigation controller since you’d be overriding the values set by automaticallyAdjustsScrollViewInsets.


#3

Thanks Christian,

That is the solution that I ended up with after making my initial post. Thanks for the confirmation!


#4

This method seemed a lot cleaner, but apparently only works for iOS 7.

[code]// This moves it down so its not overwriting the status bar

  • (void) viewDidLayoutSubviews {
    // only works for iOS 7+
    if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 7.0) {
    CGRect viewBounds = self.view.bounds;
    CGFloat topBarOffset = self.topLayoutGuide.length;

      // snaps the view under the status bar (iOS 6 style)
      viewBounds.origin.y = topBarOffset * -1;
      
      // shrink the bounds of your view to compensate for the offset
      viewBounds.size.height = viewBounds.size.height + (topBarOffset * -1);
      self.view.bounds = viewBounds;
    

    }
    }[/code]

Does the status bar automatically push the content down pre iOS 7?


#5

[quote=“ChristianKeur”]You can manually add some padding to the top of the header view to accomplish this. Here is a potentially cleaner approach:

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    self.tableView.contentInset = UIEdgeInsetsMake(20, 0, 0, 0);
    self.tableView.scrollIndicatorInsets = UIEdgeInsetsMake(20, 0, 0, 0);
}

[/quote]

This didn’t quite work for me. It did solve the top-padding problem to match up with Fig 8.2 in the book, but then once you get to Chapter 9, when you have more items and you scroll up, the scrolled content overwrites the top status bar.


#6

[quote=“slooker”]This method seemed a lot cleaner, but apparently only works for iOS 7.

[code]// This moves it down so its not overwriting the status bar

  • (void) viewDidLayoutSubviews {
    // only works for iOS 7+
    if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 7.0) {
    CGRect viewBounds = self.view.bounds;
    CGFloat topBarOffset = self.topLayoutGuide.length;

      // snaps the view under the status bar (iOS 6 style)
      viewBounds.origin.y = topBarOffset * -1;
      
      // shrink the bounds of your view to compensate for the offset
      viewBounds.size.height = viewBounds.size.height + (topBarOffset * -1);
      self.view.bounds = viewBounds;
    

    }
    }[/code]
    [/quote]

Like Christian’s solution, this works for Chapter 8 but not for Chapter 9. Once the “New” functionality is added in Chapter 9, this gets called every time I add a new item, so the top padding keeps increasing with every addition. If I set a static variable to make sure it only gets executed once, then it doesn’t have any effect at all - apparently it needs to execute on every subview load except after an item add.

If I understand the thread correctly, once one gets to Chapter 10 and the UINavigationController, the issue will go away. Still, it would be nice to have a canonical solution that works for Chapter 8 and Chapter 9.