繁体   English   中英

UIViewController容器:允许孩子使用不同的方向

[英]UIViewController container: allow different orientations for childs

更新:是否可以将一个UIViewControllervc1 )作为容器并将一个UIViewController作为子vc2vc2

[vs1 addChildViewController:vc2];
[vs1.view addSubview:vc2.view];

并允许vc2方向更改独立于vc1支持的方向?

Fe vc1仅支持纵向方向,而vc2支持所有方向。 我想看到vc2视图旋转为横向,而vc1视图仍为纵向。

Fe我有VC1和VC2(弹出窗口)

肖像

VC1仅支持纵向方向,而VC2支持所有方向。

在此处输入图片说明

我想让横向显示弹出窗口

在此处输入图片说明

方向由父视图控制器检测。 我使用的方法是在父视图控制器中,检测哪个视图是当前活动视图,然后根据该方向进行定向。

抱歉,标准行为无法实现您想要的。 当childViewController支持某个方向时,其父级和当前层次结构中的所有其他ViewController也必须支持该方向。

您应该做的是注册parentViewController来接收设备旋转通知,然后根据这些通知可以转换childViewController的视图。 因此,基本上,您的应用程序将始终保持纵向模式,但是childViewController的视图将被转换为看起来像是横向的。

注册设备定向通知,如下所示

[[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications];
[[NSNotificationCenter defaultCenter] addObserver: self selector: @selector(deviceDidRotate:) name: UIDeviceOrientationDidChangeNotification object: nil];

然后这样处理

// This method gets called everytime the device (!), not the viewController rotates
-(void)deviceDidRotate: (NSNotification*) notification
{
    UIDeviceOrientation orientation = [[UIDevice currentDevice] orientation];

    // Animate the transform here based on the orientation
}

请记住,UIDeviceOrientation和UIInterfaceOrientation是相反的。 UIDeviceOrientationLandscapeLeft = UIInterfaceOrientationLandscapeRight。 该设备向左旋转,因此用户界面必须向右旋转。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM