How come some of my UIViews are shifted after navigation?

前端 未结 5 1267
广开言路
广开言路 2020-12-14 12:29

In some of my application designs or for just some UIViews, following a navigationController\'s pushViewController, my new view will be shifted off the window by the height

5条回答
  •  臣服心动
    2020-12-14 12:49

    I've used Apple's NavBar sample code to try and reproduce this problem.

    the applicationDidFinishLaunching is originally implemented like this:

    [window addSubview:navigationController.view];
    [window makeKeyAndVisible];
    

    If I change it to this:

    UIViewController *shellController = [[UIViewController alloc] initWithNibName:nil bundle:nil];
    [shellController.view addSubview:navigationController.view];
    [window addSubview:shellController.view];
    [window makeKeyAndVisible];
    

    Then I get the gap appearing.

    However if I only do this:

    UIView *shell = [[UIView alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    [shell addSubview:navigationController.view];
    [window addSubview:shell];
    [window makeKeyAndVisible];
    

    Then everything looks normal.

    So I guess every view controller will offset its view if its the root view controller. But the navigation controller overrides this behaviour to offset its view regardless of whether it's the root view controller. Therefore I would say your gap is appearing because you've got a navigation controller somewhere lower in the hierarchy than it's supposed to be.

提交回复
热议问题