Bi-directional storyboard travel without stacking

后端 未结 1 739
误落风尘
误落风尘 2021-01-17 06:08
  • Push segues stack view controllers, while popping \'unstacks\' them.
  • Modal segues stack view controllers, while unwinding \'unstacks\' them.

I

相关标签:
1条回答
  • 2021-01-17 06:34

    If you don't want any sort of stack, just have your perform method replace the window's root view controller with the destination view controller. The source view controller will then be deallocated (as long as you didn't create any strong reference to it).

    -(void)perform {
        UIWindow *win =[self.sourceViewController view].window;
        win.rootViewController = self.destinationViewController;
    }
    

    A better way to do this, that handles rotations and both orientations, is to add a container view (in IB) to the first controller (make it the size of the whole view), and then connect this custom segue from the embedded controller that you get when you drag in the container view. This code switches out that embedded controller for the other controller.

    -(void)perform {
        UIViewController *source = self.sourceViewController;
        UIViewController *dest = self.destinationViewController;
        UIViewController *parent = source.parentViewController;
        [parent addChildViewController:dest];
        dest.view.frame = parent.view.bounds;
    
        [source willMoveToParentViewController:nil];
        [parent transitionFromViewController:source toViewController:dest duration:.6 options:UIViewAnimationOptionTransitionCrossDissolve animations:^{}
                                completion:^(BOOL finished) {
                                    [source removeFromParentViewController];
                                    [dest didMoveToParentViewController:parent];
                                    [self constrainView:dest.view equalToView:parent.view];
                                }];
    }
    
    
    -(void)constrainView:(UIView *) childView equalToView:(UIView *) parentView {
        [childView setTranslatesAutoresizingMaskIntoConstraints:NO];
        NSLayoutConstraint *con1 = [NSLayoutConstraint constraintWithItem:childView attribute:NSLayoutAttributeCenterX relatedBy:0 toItem:parentView attribute:NSLayoutAttributeCenterX multiplier:1 constant:0];
        NSLayoutConstraint *con2 = [NSLayoutConstraint constraintWithItem:childView attribute:NSLayoutAttributeCenterY relatedBy:0 toItem:parentView attribute:NSLayoutAttributeCenterY multiplier:1 constant:0];
        NSLayoutConstraint *con3 = [NSLayoutConstraint constraintWithItem:childView attribute:NSLayoutAttributeWidth relatedBy:0 toItem:parentView attribute:NSLayoutAttributeWidth multiplier:1 constant:0];
        NSLayoutConstraint *con4 = [NSLayoutConstraint constraintWithItem:childView attribute:NSLayoutAttributeHeight relatedBy:0 toItem:parentView attribute:NSLayoutAttributeHeight multiplier:1 constant:0];
        NSArray *constraints = @[con1,con2,con3,con4];
        [parentView addConstraints:constraints];
    }
    

    enter image description here

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