我正在尝试创建一个自定义segue,以便在任何方向上以线性(平面)层次结构在VC之间移动,而不会在任一方向上堆叠。似乎在VC之间的两个方向都有隔离可以防止被解雇。
有什么想法吗?
答案 0 :(得分:0)
如果您不想要任何类型的堆栈,只需让您的perform方法用目标视图控制器替换窗口的根视图控制器。然后将释放源视图控制器(只要您没有创建任何强引用)。
-(void)perform {
UIWindow *win =[self.sourceViewController view].window;
win.rootViewController = self.destinationViewController;
}
处理旋转和两个方向的更好的方法是将容器视图(在IB中)添加到第一个控制器(使其成为整个视图的大小),然后连接此自定义segue在容器视图中拖动时获得的嵌入式控制器。此代码将为其他控制器切换该嵌入式控制器。
-(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];
}