iPhone - 允许横向取向只有一个viewcontroller [英] iPhone - allow landscape orientation on just one viewcontroller

查看:121
本文介绍了iPhone - 允许横向取向只有一个viewcontroller的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个基于导航的应用程序,其中我想只有一个视图控制器支持横向。对于viewController(vc1),在shouldAutorotate中,我为所有方向返回YES,而在其他控制器中,我只返回YES,只有纵向模式



设备处于横向模式,我从vc1进入下一个屏幕,下一个屏幕也以横向模式旋转。我假设如果我只为纵向模式返回YES,屏幕将只显示为纵向。



这是预期的行为吗?

解决方案

p>如果使用UIViewController的 shouldAutorotateToInterfaceOrientation 方法,则您不能仅支持其中一个视图控件的横向方向。

您只有两个选择:是否所有视图控件都支持横向没有视图控制器支持它。



如果您只想支持一个环境,您需要检测设备旋转,并手动旋转视图控制器中的视图。

您可以使用通知检测设备轮换。

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

然后,当您检测到设备旋转时,您可以旋转视图。

   - (void)didRotate:(NSNotification *)notification {
UIDeviceOrientation orientation = [[notification object] orientation];

if(orientation == UIDeviceOrientationLandscapeLeft){
[xxxView setTransform:CGAffineTransformMakeRotation(M_PI / 2.0)];
} else if(orientation == UIDeviceOrientationLandscapeRight){
[xxxView setTransform:CGAffineTransformMakeRotation(M_PI / -2.0)];
} else if(orientation == UIDeviceOrientationPortraitUpsideDown){
[xxxView setTransform:CGAffineTransformMakeRotation(M_PI)];
} else if(orientation == UIDeviceOrientationPortrait){
[xxxView setTransform:CGAffineTransformMakeRotation(0.0)];
}
}


I have a navigation-based application in which I would like just one of the viewcontrollers to support landscape orientation. For that viewcontroller (vc1), in shouldAutorotate, I am returning YES for all orientations and in the other controllers I am returning YES only for portrait mode

But even then if the device is in landscape mode and I go to the next screen from vc1, the next screen also comes up rotated in landscape mode. I assumed that if I return a YES only for portrait mode, the screen should show up only in portrait.

Is this the expected behavior? How do I achieve what I am looking for?

Thanks.

解决方案

You can't support the landscape orientation only for one of the viewcontrollers if you use shouldAutorotateToInterfaceOrientation method of UIViewController.
You have only two choice whether all viewcontrollers support the landscape or no viewcontrollers support it.

If you want to support the landscape only for one, You need to detect device rotation and manually rotate views in the viewcontroller.
You can detect the device rotation by using Notification.

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

Then, you can rotate your views when you detect the device rotation.

- (void)didRotate:(NSNotification *)notification {
    UIDeviceOrientation orientation = [[notification object] orientation];

    if (orientation == UIDeviceOrientationLandscapeLeft) {
        [xxxView setTransform:CGAffineTransformMakeRotation(M_PI / 2.0)];
    } else if (orientation == UIDeviceOrientationLandscapeRight) {
        [xxxView setTransform:CGAffineTransformMakeRotation(M_PI / -2.0)];
    } else if (orientation == UIDeviceOrientationPortraitUpsideDown) {
        [xxxView setTransform:CGAffineTransformMakeRotation(M_PI)];
    } else if (orientation == UIDeviceOrientationPortrait) {
        [xxxView setTransform:CGAffineTransformMakeRotation(0.0)];
    }
}

这篇关于iPhone - 允许横向取向只有一个viewcontroller的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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