objective-c - iOS如何指定某个页面可以旋转屏幕,其余控制器都正常竖屏?
黄舟
黄舟 2017-04-18 09:14:18
[iOS讨论组]

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

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

黄舟
黄舟

人生最曼妙的风景,竟是内心的淡定与从容!

全部回复(7)
ringa_lee

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

伊谢尔伦

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

伊谢尔伦

在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;就可以了

天蓬老师

一开始我也向上面那样设置了,但是后来显示的结果还是不对,虽然界面是竖屏显示,但是整体还是横屏的,状态栏之类的还是在横屏。后来参考了下面的方法解决了:
http://www.devp.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;
    }
}
怪我咯

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

  • (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
    {
    return NO;
    }

  • (BOOL)shouldAutorotate
    {
    return NO;
    }

  • (UIInterfaceOrientationMask)supportedInterfaceOrientations
    {
    return UIInterfaceOrientationMaskAll;
    }

怪我咯

你是如何解决的

PHPz

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

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

    override func supportedInterfaceOrientations() -> UIInterfaceOrientationMask {
        return .AllButUpsideDown
    }
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 意见反馈 讲师合作 广告合作 最新更新 English
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号