双向故事板旅行无需堆叠 [英] Bi-directional storyboard travel without stacking

查看:131
本文介绍了双向故事板旅行无需堆叠的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


  • 推送segues堆栈视图控制器,同时弹出'unstacks'。

  • Modal segues堆栈视图控制器,同时展开'unstacks'它们。

我正在尝试创建一个自定义segue,以在任意方向上的线性(平面)层次结构中的VC之间移动,而不在任一方向上堆叠。似乎在VC之间的两个方向都有隔离可以防止被解雇。

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.

任何想法?

推荐答案

如果您不想要任何类型的堆栈,只需让您的perform方法用目标视图控制器替换窗口的根视图控制器。然后将释放源视图控制器(只要您没有创建任何强引用)。

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

执行此操作的更好方法是处理旋转和两个方向,将容器视图(在IB中)添加到第一个控制器(使其成为整个视图的大小),然后在嵌入容器视图中时从嵌入式控制器连接此自定义segue。此代码将为其他控制器切换该嵌入式控制器。

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

这篇关于双向故事板旅行无需堆叠的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆