开发者

How does presentModalViewController interact with nested UITabBarController and UINavigationController

开发者 https://www.devze.com 2022-12-10 04:07 出处:网络
I have a view that I want to take up the full screen, so I override the init method, and some of the view methods:

I have a view that I want to take up the full screen, so I override the init method, and some of the view methods:

- (id) init {
  if (self = [super init]) {
    self.wantsFullScreenLayout = YES;
  }
  return self;
}
- (void) viewWillAppear:(BOOL)animated {
  [super viewWillAppear:animated];
  [[UIApplication sharedApplication] setStatusBarHidden:YES animated:YES];
}
- (void) viewWillDisappear:(BOOL)animated {
  [super viewWillDisappear:animated];
  [[UIApplication sharedApplication] setStatusBarHidden:NO animated:YES];
}

Now, from another screen, I want to display it as a modal view:

UIViewController *screen = [[MyScreen alloc] init];
[self presentModalViewController:screen];
[screen release];

All pretty standard stuff. When I want the full-screen view to go away, however, the previous view is shifted or stretched up by about 40 pixels.

Specificially, I have a UITabBarController with a UINavigationController inside, displaying a UITableViewController, which is the view that displays the subview, and also the view that gets shifted up. If the table is not in a navigation controller, everything works just fine, nothing gets shifted up at all. If I experiment with commenting out the wantsFullScreenLayout and setStatusBarHidden lines with no navigation bar, it sometimes shifts up just 20 pixels, or doesn't actually display on the full screen (but later it does without ch开发者_如何转开发anging any code), or sometimes doesn't break at all (but I am not getting the full full screen with any of these)

What am I doing wrong?


Through some combination of Sean's suggestion and jumping up the responder chain, I've found a solution that works is what seems like all circumstances (so far).

First issue: The Table View by itself does not display in a navigation controller, but may show up in one if being selected from the more view in the tab bar, and that's the case where displaying the modal view in full screen causes the table to underlap the navigation bar upon return.

Second issue: When not displayed in a navigation controller, presenting the modal view does not take up the full screen (even though wantsFullScreenLayout is set to YES). When returning from this view, the view is shifted up by 20 pixels and you can see a gap between the bottom of the table and the top of the tab bar.

Solution:

- (void) viewWillAppear:(BOOL)animated {
  [super viewWillAppear:animated];
  [self.navigationController setNavigationBarHidden:YES animated:NO];
  [self.navigationController setNavigationBarHidden:NO animated:NO];
}

- (void) presentModalViewController:(UIViewController *)screen animated:(BOOL)animated {
  UIResponder *responder = self;
  while (responder && ![responder isKindOfClass:[UITabBarController class]]) {
    responder = [responder nextResponder];
  }
  [(UIViewController *)responder presentModalViewController:screen animated:YES];
}

The toggling of the navigation bar's visibility forces the relayout. Overriding presentModalViewController actually calls presentModalViewController on the tab bar controller instead, which then causes it to show in the full screen. For some reason, self.tabBarController is nil when not in the more view controller, so I had to jump up the responder chain to find it.


Your UINavigationController will get called with the viewWillAppear before the modal view is dismissed. Have you tried calling [[UIApplication sharedApplication] setStatusBarHidden:NO animated:NO]; inside the controllers that can be visible post modal dismissal. I have run into tons of problems displaying modal views on top of UINavigationControllers when bounds change. It fights any layout changes and requires lots of resetting to previous states to get it behaving nicely. It might also not hurt to call [self.navigationController setNavigationBarHidden:NO animated:NO] as well to force layout.

If this works well it might serve you to create a simple baseclass that sets these in it's viewWillAppear and then just subclass it for all non modal view controllers.

If this doesn't work you might try placing a swap view at the top level that contains the tab bar controller and then you could remove the tab bar controller with a transition when you present your modal view. Yes this isn't technically modal but would still look nice and offer the same effect. At that time since the view controller is out of the view hierarchy it shouldn't get it's layout all munged.


I think this has to do with the timing of the presentModalViewController: call. As a test you could try adding sleep(3) before you call that method. If that fixes anything, or even if it doesn't i guess I would try moving the order of things around. maybe viewDidDisappear and viewDidAppear instead of 'Will'

0

精彩评论

暂无评论...
验证码 换一张
取 消