当父视图控制器不启用时,swift – 子视图控制器旋转

当父视图控制器不启用时,swift – 子视图控制器旋转,第1张

概述我正在努力做什么: 由父视图控制器管理的父视图不应旋转. 由Child View Controller管理的子视图应该向所有方向旋转. 我试过的 ParentViewController override func supportedInterfaceOrientations() -> UIInterfaceOrientationMask { return .Portrait}ove 我正在努力做什么:

由父视图控制器管理的父视图不应旋转.

由Child VIEw Controller管理的子视图应该向所有方向旋转.

我试过的

ParentVIEwController

overrIDe func supportedInterfaceOrIEntations() -> UIInterfaceOrIEntationMask {    return .Portrait}overrIDe func shouldautorotate() -> Bool {    return true}fun addChildVIEwController() {    let storyBoard = UIStoryboard(name: "Main",bundle: nil)    self.childVIEwController = storyBoard("Child VIEw Controller") as? ChildVIEwController    self .addChildVIEwController(self.childVIEwController!)    self.childVIEwController! .dIDMovetoParentVIEwController(self)    self.vIEw .addSubvIEw(self.childVIEwController!.vIEw)    self.childVIEwController!.vIEw.frame = CGRectMake (40,200,400,250)}

ChildVIEwController

overrIDe func supportedInterfaceOrIEntations() -> UIInterfaceOrIEntationMask {    return  .All}overrIDe func shouldautorotate() -> Bool {    return true}

Xcode部署信息中的支持方向设置为全部四个.

我得到的

VIEw中没有一个旋转.如果我将父项的旋转设置为all,则所有视图都会一起旋转.所以这是全无的.

UPDATE

当我尝试为UIDeviceOrIEntationDIDChangeNotification放置一个观察者并使用UIDevice.currentDevice().orIEntation使用CGAffainetransformMakeRotate来旋转childVIEw时,我得到所需的结果.然而,如果我旋转到横向,并尝试拉下通知中心(或者如果我得到一个系统通知),这仍然是纵向(因为应用程序本来还留在肖像),旋转的childVIEw旋转回到肖像,以纪念状态栏/通知/通知中心.

股票iOS相机应用程序是我可以呈现的最好的例子.主画布不会旋转到不同的方向,而状态栏在幕后旋转,使设备旋转.而且,他们自己也在轮流中回顾不同的方向.我正在努力实现这一行为….

在相机应用程序中实现的效果类似于以下组合:

> Technical Q&A QA1890中描述的用于防止旋转视图的技术;和
>使用子级UIStackVIEws重新排列设备旋转的子视图(例如,参见WWDC 2016 Session 233中AdaptiveElements示例代码中的SimpleExampleViewController类).

技术说明说明,自转的底层实现包括直接将应用转换到应用程序的窗口:

autorotation is implemented by applying a rotation transform to the application’s window when the system determines that the interface must rotate. The window then adjusts its bounds for the new orIEntation and propagates this change down through the vIEw controller hIErarchy via calls to each vIEw controller’s and presentation controller’s vIEwWillTransitionToSize:withTransitionCoordinator: method.

请注意,相机应用程序不会选择不使用自动分类:在使用相机应用程序时,“通知中心”和“控制中心”的方向反映了设备方向.相机应用程序中只有特定的视图才会选择不使用自动转换功能.实际上,this answer表示,这样的应用程序通常会使用AVFoundation类(如AVCaptureVideoPreviewLayer和AVCaptureConnection)提供的vIDeoGravity和vIDeoOrIEntation属性.

您应该采取的方法取决于您是否只希望停止父视图旋转或是否要停止容器视图控制器(如UINavigationController)从旋转(即锁定设备方向).

如果是前者,请使用技术说明中描述的技术来修复父视图的方向,(对于iOS 9),在UIStackVIEw中旋转子视图,并使用axis属性重新排列设备旋转的子视图,或(对于iOS 8)手动旋转设备轮播上的子视图(参见this answer和this sample code).

如果是后者,你所采取的方法是正确的.有关示例代码,请参阅this answer.您需要共享您的代码和有关视图控制器层次结构的更多信息,以便我们诊断涉及Notification Center的怪癖.

附录:Technical Q&A QA1688中已解释了调用toautorotate和supportedInterfaceOrIEntations未传播到子视图控制器的原因:

Beginning with iOS 6,only the topmost vIEw controller (alongsIDe the UIApplication object) participates in decIDing whether to rotate in response to a change of the device’s orIEntation. More often than not,the vIEw controller displaying your content is a child of UINavigationController,UITabbarController,or a custom container vIEw controller. You may find yourself needing to subclass a container vIEw controller in order to modify the values returned by its supportedInterfaceOrIEntations and shouldautorotate methods.

在斯威夫特,你可以override those methods by using extensions.

总结

以上是内存溢出为你收集整理的当父视图控制器不启用时,swift – 子视图控制器旋转全部内容,希望文章能够帮你解决当父视图控制器不启用时,swift – 子视图控制器旋转所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://www.outofmemory.cn/web/1039705.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-24
下一篇 2022-05-24

发表评论

登录后才能评论

评论列表(0条)

保存