视图控制器之间的转换和iOS中的旋转(Transition between view controllers and rotation in iOS)

编程入门 行业动态 更新时间:2024-10-22 20:30:10
视图控制器之间的转换和iOS中的旋转(Transition between view controllers and rotation in iOS)

考虑一个带有两个子视图控制器(A和B)的容器视图控制器,它们都添加了addChildViewController: . 然后:

A.view被添加到容器视图 B通过从A到B执行transitionFromViewController来显示。B接收到viewWillLayoutSubviews并且所有对世界都很好。 设备在显示B时旋转。只有B接收旋转调用( willRotateToInterfaceOrientation: et all)。 A通过从B到A执行transitionFromViewController来显示.A没有收到viewWillLayoutSubviews ,因此布局被破坏。

这是预期的行为? 如果不是,我可能会做错什么? 如果是,我应该怎么做才能在显示B时通知A旋转变化?

Consider a container view controller with two child view controllers (A and B), both added with addChildViewController:. Then:

A.view is added to the container view B is displayed by doing transitionFromViewController from A to B. B receives viewWillLayoutSubviews and all is good with the world. The device rotates while displaying B. Only B receives the rotation calls (willRotateToInterfaceOrientation: et all). A is displayed by doing transitionFromViewController from B to A. A doesn't receive viewWillLayoutSubviews and thus the layout is broken.

Is this the expected behavior? If not, what might I be doing wrong? If yes, what should I do to notify A of the rotation change while displaying B?

最满意答案

只要你调用addChildViewController:你现在是一个View Controller容器实现者。 这意味着你必须比标准的表示调用(如presentViewController..做更多的工作。 这包括处理您作为孩子添加的控制器视图的帧,正如您的问题所暗示的那样。

例如,要实现一个只显示每个子屏幕全屏的超级基本示例容器,您可以执行类似这样的操作。

-(void)swapChildVCFrom:(UIViewController *)from to:(UIViewController *)to{ [self addChildViewController:to]; [from willMoveToParentViewController:nil]; // Adjust the new child view controller's view's frame // For example here just set it to fill the parent view to.view.frame = self.view.bounds; [self transitionFromViewController:from toViewController:to duration:1.0 options:UIViewAnimationOptionTransitionFlipFromLeft animations:nil completion:^(BOOL b){ [to didMoveToParentViewController:self]; [from.view removeFromSuperview]; [from removeFromParentViewController]; }]; }

As soon as you call addChildViewController: you are now a View Controller Container Implementer. This means you do have to do a little more work than a standard presentation call like presentViewController... This includes dealing with the frames of the views of the controllers you add as children, as your question suggests you might have expected.

For example, to implement a super basic example container, that just shows each child full screen, you could do something like this.

-(void)swapChildVCFrom:(UIViewController *)from to:(UIViewController *)to{ [self addChildViewController:to]; [from willMoveToParentViewController:nil]; // Adjust the new child view controller's view's frame // For example here just set it to fill the parent view to.view.frame = self.view.bounds; [self transitionFromViewController:from toViewController:to duration:1.0 options:UIViewAnimationOptionTransitionFlipFromLeft animations:nil completion:^(BOOL b){ [to didMoveToParentViewController:self]; [from.view removeFromSuperview]; [from removeFromParentViewController]; }]; }

更多推荐

本文发布于:2023-07-31 01:20:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1340480.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:视图   控制器   iOS   Transition   controllers

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!