首页 > iOS如何指定某个页面可以旋转屏幕,其余控制器都正常竖屏?

iOS如何指定某个页面可以旋转屏幕,其余控制器都正常竖屏?

需求是这样的:
整个App的大部分界面都是竖屏显示,且只允许竖屏显示。
只有单独几个页面可以切换横竖屏,比如视频播放页面,图片浏览界面。

按照网上的方法试了好久,都没成功,求助!!!


一开始我也向上面那样设置了,但是后来显示的结果还是不对,虽然界面是竖屏显示,但是整体还是横屏的,状态栏之类的还是在横屏。后来参考了下面的方法解决了:
http://www.devdiv.com/iOS_iPhone-ios_xcode_-thread-211995-1-1.html

//点击按钮旋转到横屏
- (void)switchToLandscape
{
    [[UIDevice currentDevice] setValue:[NSNumber numberWithInteger:UIInterfaceOrientationPortrait] forKey:@"orientation"];
    //上一句话是防止手动先把设备置为横屏,导致下面的语句失效.
    [[UIDevice currentDevice] setValue:[NSNumber numberWithInteger:UIDeviceOrientationLandscapeLeft] forKey:@"orientation"];
}

//点击返回旋转到竖屏
- (void)switchToPortrait
{
    [[UIDevice currentDevice] setValue:[NSNumber numberWithInteger:UIDeviceOrientationLandscapeLeft] forKey:@"orientation"];
    //上一句话是防止手动先把设备置为竖屏,导致下面的语句失效.
    [[UIDevice currentDevice] setValue:[NSNumber numberWithInteger:UIDeviceOrientationPortrait] forKey:@"orientation"];
}

这个我写在了viewController里面,在跳转里面对应设置就好了。
其他答案中提到的那2个函数也要对应添加:

- (BOOL)shouldAutorotate
{
    return YES;
}

- (UIInterfaceOrientationMask)supportedInterfaceOrientations
{
    if (_isAllowRotation == YES) {
        return UIInterfaceOrientationMaskLandscape;
    } else {
        return UIInterfaceOrientationMaskPortrait;
    }
}

参考问题:https://.com/q/1010000004486783/a-1020000004487227


我自己搞了搞,写出来一个,但总感觉这样不太科学。。。
具体demo在github上:
https://github.com/iDvel/iOS-rotate-demo


关于设备的旋转方法,前面两个返回NO,就不支持旋转了,最后一个支持的旋转方向


你是如何解决的


在AppDelegate.m中

- (NSUInteger)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window
{
    if (_allowRotation == 1) {
        return UIInterfaceOrientationMaskAll;
    }else{
        return (UIInterfaceOrientationMaskPortrait);
    }
}

// 支持设备自动旋转
- (BOOL)shouldAutorotate
{
    if (_allowRotation == 1) {
        return YES;
    }
    return NO;
}

写这两个方法

在你要旋转的controller中一开始的地方写这两句就可以了

_appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate; _appDelegate.allowRotation = 1;

在要这个controller要消失的时候 写_appDelegate.allowRotation = 0;就可以了


在工程设置里面选择Portrait,就像这样:

然后在可以旋转的视图的ViewController里面加上:

    override func supportedInterfaceOrientations() -> UIInterfaceOrientationMask {
        return .AllButUpsideDown
    }
【热门文章】
【热门文章】