Not sure why UIView is being nudged up by around 1

2019-01-17 23:09发布

问题:

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.

回答1:

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.



回答2:

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~ :)



回答3:

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.



回答4:

Looks like you call pushVC to this UIViewController in the wrong place. I had the same problem. It was solved by moving [self.navigationController pushViewController: animated:] from viewDidLoad to viewDidAppear: in a previous UIViewController in hierarchy.



回答5:

Whenever a weird gap appears, or the view is partly hidden behind the status bar, it has something to do with either

  1. shouldAutorotate definition in your root controller - not sure if it is a bug or not, but try to stay consistent with the return of that function for all sub-viewcontrollers (usually results in a viewcontroller being hidden behind the status bar)
  2. a wantsFullscreen definition of a subview - set to NO if possible

i had both bugs in my app and solved them! Hope I could save you some time (I wasted a lot on it)