Skip navigation

iPad status bar covers up my app's top  UIViews; okay after rotation

8053 Views 30 Replies Latest reply: Aug 20, 2013 5:15 AM by Erobe2591 RSS
1 2 3 Previous Next
David Restler Calculating status...
Currently Being Moderated
Nov 23, 2010 9:40 AM
I wrote an iPad app. When it is first started up (in iPhone simulator, or on real iPad) all the UIViews at the top of my window are partially covered-up by the iPads time/battery status bar.

After I rotate the iPad (simulator or real one) the problem goes away: my UIViews appear shifted down, so they are under (below) the status bar. In other words, after I do the first rotation, the iPad knows to push my window downwards about 20 pixels to make room for the status bar. But before the first rotation (simulator or hardware) the iPad is not shifting my UIViews downward.

Question: What can I do in software (during the startup logic) to get the iPad to push my window down to make room for the status bar?

I think the iPad should do it automatically for me, but since it is not, I'm willing to take some action in the software to get it to happen. Any thoughts? NOTE: When I create all my UIViews, I'm using a coordinate of (0,0) for my upper left corner, which is correct, and after the first rotation everything works great. Also: I have auto-rotation enabled, so my app is always rotating to keep its display "up" so users can view it in all 4 rotations.
iPad, iPhone OS 3.1.3
  • xnav Level 5 Level 5 (6,615 points)
    Did you include a +Status Bar+ in the 'Simulated user interface elements' when you built the view in IB? This doesn't enforce anything at execution but prevents you from adding interface elements that would be covered by it.
    Mac Pro 2x3GHz Dual, Mac OS X (10.6.5), BlueTooth Kybd, Trackpad, BootCamp WinXP, iPhone, iPad
  • xnav Level 5 Level 5 (6,615 points)
    OK, what are you setting the frame to.
    Mac Pro 2x3GHz Dual, Mac OS X (10.6.5), BlueTooth Kybd, Trackpad, BootCamp WinXP, iPhone, iPad
  • xnav Level 5 Level 5 (6,615 points)
    Try (0,0,768,1004).
    Mac Pro 2x3GHz Dual, Mac OS X (10.6.5), BlueTooth Kybd, Trackpad, BootCamp WinXP, iPhone, iPad
  • RayNewbie Level 5 Level 5 (6,810 points)
    Hi David -
    David Restler wrote:
    Question: What can I do in software (during the startup logic) to get the iPad to push my window down to make room for the status bar?

    If you're creating the view in your code, you're explicitly setting the view frame. The controller then assumes you know what you're doing and doesn't make any adjustment for the status bar (or any other bars that may be above or below the content area).

    In other words, when you explicitly set the frame in loadView, you're buying sole responsibility for the starting dimensions. In the case of an iPad with only the status bar, you need to drop the y-origin down 20, and shorten the height by 20:

    - (void)loadView {
    CGRect frame = CGRectMake(0, 20, 768, 1004);
    UIView *v = [[UIView alloc] initWithFrame:frame];
    self.view = v;
    [v release];
    NSLog(@"%s: self.view=%@ self.view.frame=%@",
    _func_, self.view, NSStringFromCGRect(self.view.frame));
    }

    Btw, if you ever open your IB, try adding and subtracting the various simulated bars while watching the dimensions of the view in the Size Inspector. If the view is the main content view (i.e. connected to the 'view' outlet of File's Owner), you should see the y-origin and size adjust for each combination of bars. In fact, those dimensions will probably be grayed out, since IB doesn't trust us with such critical numbers. The point is, that when a view is loaded from a nib built with the correct simulated bars, the adjustment for the bars has already been saved in that file.
    I think the iPad should do it automatically for me

    If you want to see what the controller does by default (i.e. no nib and no frame defined in loadView), try this code:

    // MyAppDelegate.m
    #import "MyAppDelegate.h"
    #import "MyViewController.h"
    @implementation MyAppDelegate
    @synthesize window, viewController;

    - (void)applicationDidFinishLaunching:(UIApplication *)application {
    MyViewController *vc = [[MyViewController alloc] initWithNibName:nil bundle:nil];
    vc.view.backgroundColor = [UIColor grayColor];
    self.viewController = vc;
    [vc release];
    [window addSubview:viewController.view];
    [window makeKeyAndVisible];
    NSLog(@"%s: viewController.view.frame=%@",
    _func_, NSStringFromCGRect(viewController.view.frame));
    }
    - (void)dealloc {
    [viewController release];
    [window release];
    [super dealloc];
    }
    @end

    // MyViewController.m
    #import "MyViewController.h"
    @implementation MyViewController

    /*
    // Implement loadView to create a view hierarchy programmatically, without using a nib.
    - (void)loadView {
    }
    */
    /*
    // Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
    - (void)viewDidLoad {
    [super viewDidLoad];
    }
    */
    @end

    When no nib name is specified, and the view loader can't find a nib that matches the owner's class name, and loadView isn't overridden, the view controller creates a default view (see no. 3 under "The steps that occur during the load cycle are as follows:" in [Understanding the View Management Cycle|http://developer.apple.com/library/ios/featuredarticles/ViewControllerPGf oriPhoneOS/BasicViewControllers/BasicViewControllers.html#//apple_ref/doc/uid/TP 40007457-CH101-SW19] in the View Controller Programming Guide for iOS). If you run the above example code, you should see the frame of the default view takes all bars into account.
    ... I'm using a coordinate of (0,0) for my upper left corner, which is correct

    As explained above, it's not correct if there are any upper bars
    and after the first rotation everything works great.

    Yes, all bets are off after an auto-rotation. The system recalculates the content view frame in that case (though that math doesn't always come out the way you want).

    Hope that helps to unscramble things a little!
    - Ray
    iMac, Mac OS X (10.5.8)
  • xnav Level 5 Level 5 (6,615 points)
    Ray, I recommended (0,0,768,1004) because of this:
    Create a new iPad View based project called 'frame'.
    Double click on frameViewController.xib to start IB.
    Select View and examine its dimensions with the Size Inspector.
    Note: the View is created with a simulated status bar.

    ??
    Mac Pro 2x3GHz Dual, Mac OS X (10.6.5), BlueTooth Kybd, Trackpad, BootCamp WinXP, iPhone, iPad
  • RayNewbie Level 5 Level 5 (6,810 points)
    Hey X -
    xnav wrote:
    Create a new iPad View based project called 'frame'.
    Double click on frameViewController.xib to start IB.
    Select View and examine its dimensions with the Size Inspector.

    Yup, I see. But next log the frame in viewDidLoad. I think you'll see (0,20,768,1004). Else if the frame is (0,0,768,1004) in viewDidLoad, I think you'll see a 20 pixel gap between the bottom of the view and the bottom of the window. I've seen both outcomes at runtime when the Size Inspector has shown the view origin to be (0,0). I.e., when the status bar is showing, the controller either knows or doesn't know to push the view down 20, and I"ve never been able to find out when it knows and when it doesn't

    For example in an iPhone project, when starting with a clean View-based template, the Size Inspector shows the view origin at (0,0), but by the time viewDidLoad runs, the origin has been consistently pushed down 20. But this won't always be the case when I construct my own xib. Sometimes IB locks the origin at (0,0) and it isn't pushed down at runtime, in which case I"ve had to make the correction manually in viewDidLoad.

    The reason I recommended (0,20,768,1004), i.e. lowering the y-origin 20 pixels, is that I don't think the controller will correct the origin when it's been explicitly set in loadView. It seems like the controller respects a loadView setting, but may or may not trust a (0,0) origin in an xib.

    I don't mean to pass off this hypothesis as knowledge, but so far it's the only one that seems to fit my experience. I think I love black boxes as much as the next OO fan boy.. but now and then it might be nice to know what the freaking controller is actually doing.

    \- Ray
    iMac, Mac OS X (10.5.8)
  • xnav Level 5 Level 5 (6,615 points)
    I think I love black boxes as much as the next OO fan boy.. but now and then it might be nice to know what the freaking controller is actually doing.

    Must be Voodoo OO...
    Mac Pro 2x3GHz Dual, Mac OS X (10.6.5), BlueTooth Kybd, Trackpad, BootCamp WinXP, iPhone, iPad
  • xnav Level 5 Level 5 (6,615 points)
    I"ve never been able to find out when it knows and when it doesn't

    It depends on the 'Resize View from NIB' flag in the view controller attributes in the MainWindow.xib. If checked then in viewDidLoad (0,20,768,1004) else (0,0,768,1004).
    Mac Pro 2x3GHz Dual, Mac OS X (10.6.5), BlueTooth Kybd, Trackpad, BootCamp WinXP, iPhone, iPad
  • RayNewbie Level 5 Level 5 (6,810 points)
    xnav wrote:
    It depends on the 'Resize View from NIB' flag in the view controller attributes in the MainWindow.xib.

    YAY!!! _Outstanding, X!!!_ Thanks so much. The cleverly hidden Resize View From NIB check box! Who knew?

    \- Ray
    iMac, Mac OS X (10.5.8)
  • IanFromDorking Calculating status...

    Hi David,

    I may be new to all this but I'm busy writing iPhone/Pod/Pad Universal apps and have some experience in iPhone/iPod app development.

     

    Can't you just switch the status bar off or use the application frame - thereby dodging the status bar completely?

     

    1)    [[UIApplication sharedApplication] setStatusBarHidden: TRUE];


    2)   [self setWindow: [[UIWindow alloc] initWithFrame:                         [[UIScreen mainScreen] applicationFrame]]];


    I use (1) followed by (2) in all of my iApps now and I've never had a problem.

    Hope this helps.

  • appnettech Calculating status...

    The Resize View From NIB checkbox doesn't appear to be in Xcode4. Do you happen to know what the new recommended way is to achieve this same behaviour under Xcode4? I am still searching for it, but not having much luck so far.

     

    Thanks

  • MrChadBag Level 1 Level 1 (0 points)

    It's there in XCode4 for ViewControllers.  Just click the attributes inspector.

1 2 3 Previous Next

Actions

More Like This

  • Retrieving data ...

Bookmarked By (0)

Legend

  • This solved my question - 10 points
  • This helped me - 5 points
This site contains user submitted content, comments and opinions and is for informational purposes only. Apple disclaims any and all liability for the acts, omissions and conduct of any third parties in connection with or related to your use of the site. All postings and use of the content on this site are subject to the Apple Support Communities Terms of Use.