Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

iPhone UIViewController goes under status bar

I have a UIView and a UIController view. My is standard a 320x460 view. In applicationDidFinishLaunching I do:

[window addSubview:[controller view]];

The weird thing is that the UIView goes under the status bar (like there's missing outlet). However, if I rotate iPhone to the side and then back, it shows up ok.

Is this an expected behavior (I bet I can fix it by setting offset) or am I doing smth wrong?

like image 745
Mantas Avatar asked Dec 09 '09 02:12

Mantas


4 Answers

I ran into this issue when displaying a UIViewController via presentModalViewController.

You can get around it by manually resizing the controller's view after the view has appeared:

- (void) viewDidAppear: (BOOL) animated {
    //manually adjust the frame of the main view to prevent it from appearing under the status bar.
    UIApplication *app = [UIApplication sharedApplication];
    if(!app.statusBarHidden) {
        [self.view setFrame:CGRectMake(0.0,app.statusBarFrame.size.height, self.view.bounds.size.width, self.view.bounds.size.height - app.statusBarFrame.size.height)];
    }
}
like image 119
Nick Baicoianu Avatar answered Oct 26 '22 19:10

Nick Baicoianu


I think your problem is that when you add a view to a window, you need to be aware of the state of the status bar and compensate for it:

if showing the status bar :
   [controller view].frame = CGRectMake(0, **20**, 320, 460);
else 
   [controller view].frame = CGRectMake(0, 0, 320, **480**);

this is why IB shows you a dummy status bar.

like image 44
n3wscott Avatar answered Oct 26 '22 19:10

n3wscott


I add this issue today. It turned out that I had "Wants Full Screen" checked in the ViewController's Attribute inspector.

Turning off "Wants Full Screen" resolved the problem.

like image 5
user1626682 Avatar answered Oct 26 '22 19:10

user1626682


Finally, I got to this solution. Works well for both iPhone & iPad:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{

    self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease];

    // Allocate view controller and load nib file
    if (isIPhone) {
        self.mainViewController = [[[tfdMainViewController_iPhone alloc] initWithNibName:@"tfdMainViewController_iPhone" bundle:nil] autorelease];
    } else {
        self.mainViewController = [[[tfdMainViewController_iPad alloc] initWithNibName:@"tfdMainViewController_iPad" bundle:nil] autorelease];
    }

    // Offset correction (iPhone bug?)
    CGRect r = self.mainViewController.view.frame; 
    r = CGRectOffset(r, 0, [UIApplication sharedApplication].statusBarFrame.size.height);
    [self.mainViewController.view setFrame:r];  

    [window addSubview: self.mainViewController.view];
    [self.window makeKeyAndVisible];
    return YES;
}

P.S. For some reason view has correct height: 480 (screen height in iPhone Portrait mode) - 20 (status bar) = 460, but failed to set vertical offset. It is pretty strange behavior, looks like bug.

like image 3
Mike Keskinov Avatar answered Oct 26 '22 20:10

Mike Keskinov