Bi-directional storyboard travel without stacking

百般思念 提交于 2019-12-01 12:11:42

问题


  • Push segues stack view controllers, while popping 'unstacks' them.
  • Modal segues stack view controllers, while unwinding 'unstacks' them.

I am trying to create a custom segue to move between VCs in a linear (flat) hierarchy, in either direction, without stacking in either direction. It would appear that having segues in both directions between VCs prevents dismissal.

Any ideas?


回答1:


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];
}



来源:https://stackoverflow.com/questions/20108587/bi-directional-storyboard-travel-without-stacking

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!