Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Not sure why UIView is being nudged up by around 10px

I've created a simple iPhone app which has two .xib files. In the app delegate at application did finish launching I display the first .xib file by calling:

[window addSubview:myView];

and I do the same on an IBAction for a UIButton to change the view to myView2.

What I'm finding is that there's a white bar of around 10 pixels when I run the app, for both views. I also notice that the buttons are offset by 10 pixels (approx.). So I get the impression that the view is being displayed from 10 pixels off the screen and ending short.

Any idea why this might be happening, how I can fix it, or how I can further debug what's going on? I've checked my .xib files and they are consuming the full height of the device (i.e. no white bars), so this looks to be a problem inside XCode.

EDIT: I've narrowed down the problem a little. I'm using two methods to load the subview. When I load the first view inside applicationDidFinishLaunching everything is fine. However, if I replace the two messages to window with the [self originalView] method, then everything goes a bit haywire.

- (void)applicationDidFinishLaunching:(UIApplication *)application {    
    //[self originalView];  <- I want to use this instead of the following two lines

    // Override point for customization after app launch    
    [window addSubview:viewController.view];
    [window makeKeyAndVisible];

}

-(void)endView{
    endV = [[EndViewController alloc] initWithNibName:@"EndView" bundle:nil];
    [window removeFromSuperview];
    [window addSubview:endV.view];  
}
-(void)originalView{
    viewController = [[MyAppViewController alloc] init];
    [window removeFromSuperview];
    [window addSubview:viewController.view];

}

From what I can see, I'm always calling the same lines of code, no matter if it's inside the applicationDidFinishLaunching or in [self originalView] but it seems like window is turning out to be a different object.

like image 666
mac_55 Avatar asked Jun 28 '09 08:06

mac_55


3 Answers

When using a UIViewController the normal way (i.e. pushing it on a UINavigationController), it adjusts its view's frame.

Since you're adding the subview manually, you have to adjust the frame yourself. The origin is in the upper right corner (at the top of the status bar). You want to shift your view 20 pixels down.

like image 114
Nikolai Ruhe Avatar answered Oct 16 '22 04:10

Nikolai Ruhe


Here is my solution:

// adjust the frame of subview which is going to be add
self.navController.view.frame = CGRectMake(0, 0, 320, 460);
[self.view addSubView:self.navController.view];

It works fine for me now, good luck~ :)

like image 24
firestoke Avatar answered Oct 16 '22 03:10

firestoke


In interface builder, you can add Simulated Interface Elements that will appear programatically (such as the status bar or the navigation bar). In your inspector, select your view, and go to the Attributes tab. Here you can simulate The Status bar, a top bar, or a bottom bar.

like image 41
Reed Olsen Avatar answered Oct 16 '22 04:10

Reed Olsen