Navigation bar gets adjusted after calling completeTransition: in custom transition

后端 未结 4 1002
温柔的废话
温柔的废话 2020-12-16 12:34

My goal is to provide zooming modal transition from for the user from a view similar as springboard icons zoom in when launching apps.

The presented view controller

相关标签:
4条回答
  • 2020-12-16 13:02

    The problem is that you are setting the transform before inserting the destination view controller's view into the container.

    Switching the order should fix it:

    if (self.reverse) {
        [container insertSubview:toViewController.view belowSubview:fromViewController.view];
    } else {
        [container addSubview:toViewController.view];
        toViewController.view.transform = transform;
    }
    

    See point 4 here. Since you've applied a transform prior to inserting the navigation controller's view as a subview, the layout engine doesn't think the navigation bar is at the top edge of the window, and therefore doesn't need to be adjusted to avoid the status bar.

    0 讨论(0)
  • 2020-12-16 13:04

    I've found a solution, although pretty hacky. I have to manually adjust the navigation bar frame before the animation starts:

    if (self.reverse) {
        [container insertSubview:toViewController.view belowSubview:fromViewController.view];
    } else {
        toViewController.view.transform = transform;
        [container addSubview:toViewController.view];
    
        // fix navigation bar position to prevent jump when completeTransition: is called
        if ([toViewController isKindOfClass:[UINavigationController class]]) {
            UINavigationController* navigationController = (UINavigationController*) toViewController;
            UINavigationBar* bar = navigationController.navigationBar;
            CGRect frame = bar.frame;
            bar.frame = CGRectMake(frame.origin.x, frame.origin.y + 20.0f, frame.size.width, frame.size.height);
        }
    }
    
    0 讨论(0)
  • 2020-12-16 13:23

    This is still a hack, based on Ondřej Mirtes' one but it works better if you have an in-call status bar and you're on iOS8

    if([toViewController isKindOfClass:[UINavigationController class]]) { 
      UINavigationController *navCtrl = (UINavigationController *)toViewController;
      UINavigationBar *navBar = navCtrl.navigationBar;
      if(navBar.frame.origin.y == 0 && navBar.frame.size.height == 44) {
        navBar.frame = CGRectMake(0, 0, navBar.frame.size.width, fmin(44 + [UIApplication sharedApplication].statusBarFrame.size.height, 64)); 
      }
    }
    

    Remains ugly though :/

    0 讨论(0)
  • 2020-12-16 13:24

    Add the toViewControllerView first to the ContainerView, then set the toViewControllerView transform as given below.

    [container addSubview:toViewController.view];

    toViewController.view.transform = transform;

    This will solve the problem.

    0 讨论(0)
提交回复
热议问题