views:

1376

answers:

3

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

[window addSubview:[controller view]];

What is weird, 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 ix it by setting offset) or am I doing smth wrong?

A: 

Are you manually setting the application bar hidden property AFTER adding the subview? I don't imagine this is the case, but if it's set to none when you first load the view it will layout as if there isn't one, and if you then set the status bar to not hidden it will pop up on top of your view.

A possible solution is to use [[controller view] setNeedsLayout]; after adding the subview, or possibly [window layoutSubviews];. I've never had a lot of success using those to fix layout problems, but since it works after a rotation it's worth a shot.

Ian Henry
I don't set application bar hidden at any point.Those re-layouting things didnt help...
Mantas
+2  A: 

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.

Edited: was CGRectMake(0, 20, 320, 460); Thanks for the comment.

deoryp
Thanks. I was already using this to fix the problem. I just wasn't sure if that's conventional way to fix it.
Mantas
Great fix, but side note, **20** should be the second parameter to CGRectMake, not the first :).
phooze
A: 

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)];
    }
}
Nick Baicoianu
Followup: the best solution I've seen for this is to open up the nib file for your viewcontroller, select the main view, and under "Simulated User Interface Elements" set the "Status Bar" option to anything besides "Unspecified". I can't explain what internal values are being changed, just that this solved the issue for my app. If you do this you won't need the code in my previous answer.
Nick Baicoianu