Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
342 views
in Technique[技术] by (71.8m points)

iphone - 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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...