Non-fullscreen UINavigationController

后端 未结 4 1260
臣服心动
臣服心动 2021-02-20 12:25

Is it possible to use a UINavigationController in such a way that it doesn\'t use the full window?

I\'ve tried setting it\'s view\'s frame as well as adding it\'s view t

4条回答
  •  猫巷女王i
    2021-02-20 12:56

    You cannot directly change the size of a UINavigationController or it's subviews directly, as the UINavigationController automatically resizes them to full screen, no matter what their frames are set to. The only way I've been able to overcome this so far is as follows:

    First, create an instance of UINavigationController as you normally would:

    UINavigationController *nc = [[UINavigationController alloc] init];
    self.navController = nc;
    [nc release];
    

    Then, create an instance of UIView, constrained to the size you really want:

    UIView *navView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, DESIRED_HEIGHT)];
    navView.clipsToBounds = YES;
    [navView addSubview:self.navController.view];   
    [self.view addSubview:navView];
    [navView release];
    

    The navView's clipsToBounds property must be set to YES, or the UINavigationController and it's view will still appear fullscreen. Then, add the UINavigationController to that constrained view. This UIView may then be added to the UIViewController's view, as seen above.

    The thing to note is that any UIViewController's views that are added to the UINavigationController will all have their content constrained to navView's bounds, not the frame of the subviews added to the UINavigationController, so the content in each subview should be created to properly display for the navView's bounds.

    In any case, this technique does work, as I've created an app that successfully uses it. The only other way I've ever gotten this to work is to create a custom navigation controller class from scratch, replicating the functionality of UINavigationController, but without the automatic resizing (which I've also done in the past), and that can be a pain. Hope this helps.

提交回复
热议问题