美文网首页
iOS 旋转屏的问题

iOS 旋转屏的问题

作者: Clark_new | 来源:发表于2018-07-20 11:00 被阅读0次

首先设置旋转屏


基础设置

模态视图方法:

- (BOOL)shouldAutorotate;
- (UIInterfaceOrientationMask)supportedInterfaceOrientations;
- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation;

第一个方法决定是否支持多方向旋转屏,如果返回NO则后面的两个方法都不会再被调用,而且只会支持默认的UIInterfaceOrientationMaskPortrait方向;

第二个方法直接返回支持的旋转方向,该方法在iPad上的默认返回值是UIInterfaceOrientationMaskAll,iPhone上的默认返回值是UIInterfaceOrientationMaskAllButUpsideDown,官方文档有说明

第三个方法返回最优先显示的屏幕方向,比如同时支持Portrait和Landscape方向,但想优先显示Landscape方向,那软件启动的时候就会先显示Landscape,在手机切换旋转方向的时候仍然可以在Portrait和Landscape之间切换

在UINavigationController或者自定义的UINavigationController中重写上面三个方法,即可实现在特定的控制器实现屏幕旋转代码如下:

- (BOOL)shouldAutorotate{
 // 返回当前显示的viewController是否支持旋转
    return [self.visibleViewController shouldAutorotate];
}

- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation{
// 返回当前显示的viewController支持旋转的方向
    return [self.visibleViewController preferredInterfaceOrientationForPresentation];
}

- (UIInterfaceOrientationMask)supportedInterfaceOrientations {
// 返回当前显示的viewController是优先旋转的方向
    if (![self.visibleViewController isKindOfClass:[UIAlertController class]]) {//iOS9 UIWebRotatingAlertController
        return [self.visibleViewController supportedInterfaceOrientations];
    }else{
        return UIInterfaceOrientationMaskPortrait;
    }
}

然后在想要旋转屏幕的的控制器中重写上面三个方法:

- (BOOL)shouldAutorotate {
   return YES;
}

// 这里返回需要支持旋转的方向
- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation{
    return UIInterfaceOrientationLandscapeLeft;
}
// 优先显示的方向
- (UIInterfaceOrientationMask)supportedInterfaceOrientations {
    return UIInterfaceOrientationMaskAll;
}

push控制器方法

//强制转屏
- (void)interfaceOrientation:(UIInterfaceOrientation)orientation
{
    if ([[UIDevice currentDevice] respondsToSelector:@selector(setOrientation:)]) {
        SEL selector  = NSSelectorFromString(@"setOrientation:");
        NSInvocation *invocation = [NSInvocation invocationWithMethodSignature:[UIDevice instanceMethodSignatureForSelector:selector]];
        [invocation setSelector:selector];
        [invocation setTarget:[UIDevice currentDevice]];
        int val = orientation;
        // 从2开始是因为0 1 两个参数已经被selector和target占用
        [invocation setArgument:&val atIndex:2];
        [invocation invoke];
    }
}

或者也可以这么做
在AppDelegate中添加方法关闭横竖屏切换,方法如下

1.AppDelegate.h中外露一个属性

@property(nonatomic,assign)BOOL allowRotation;//是否允许转向

2.AppDelegate.m中添加方法(如果属性值为YES,仅允许屏幕向左旋转,否则仅允许竖屏)

- (UIInterfaceOrientationMask)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(nullable UIWindow *)window

{

    if (_allowRotation == YES) {

        return UIInterfaceOrientationMaskLandscapeLeft;

    }else{

        return (UIInterfaceOrientationMaskPortrait);

    }

}

1.在需要强制横屏的控制器.m中添加旋转为横屏方法

- (void)setNewOrientation:(BOOL)fullscreen

{

if (fullscreen) {

        NSNumber *resetOrientationTarget = [NSNumber numberWithInt:UIInterfaceOrientationUnknown];

        [[UIDevice currentDevice] setValue:resetOrientationTarget forKey:@"orientation"];

        

        NSNumber *orientationTarget = [NSNumber numberWithInt:UIInterfaceOrientationLandscapeLeft];

        [[UIDevice currentDevice] setValue:orientationTarget forKey:@"orientation"];

    }else{

        NSNumber *resetOrientationTarget = [NSNumber numberWithInt:UIInterfaceOrientationUnknown];

        [[UIDevice currentDevice] setValue:resetOrientationTarget forKey:@"orientation"];

 

        NSNumber *orientationTarget = [NSNumber numberWithInt:UIInterfaceOrientationPortrait];

        [[UIDevice currentDevice] setValue:orientationTarget forKey:@"orientation"];

    }

}

2.view DidLoad中添加以下代码

AppDelegate * appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;

    appDelegate.allowRotation = YES;//(以上2行代码,可以理解为打开横屏开关)

[self setNewOrientation:YES];//调用转屏代码

3.重写导航栏返回箭头按钮,拿到返回按钮点击事件

- (void)back

{

    AppDelegate * appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;

    appDelegate.allowRotation = NO;//关闭横屏仅允许竖屏

    [self setNewOrientation:NO];

    [self.navigationController popViewControllerAnimated:YES];

}

相关文章

  • iOS 旋转屏的问题

    首先设置旋转屏 模态视图方法: 第一个方法决定是否支持多方向旋转屏,如果返回NO则后面的两个方法都不会再被调用,而...

  • 解决vue中使用element-ui中Upload上传图片后图片

    问题:ios竖屏拍摄的图片上传后会旋转90度。 解决方案: 1.使用upload的beforeUpload方法获取...

  • present一个viewController横屏与竖屏播放视频

    屏幕旋转:横屏与竖屏播放视频http://www.cocoachina.com/ios/20170329/1897...

  • iOS开发横竖屏

    关于iOS横竖屏适配 - 简书 iOS横竖屏旋转及其基本适配方法 - 梧雨北辰的博客 - CSDN博客 适配主要需...

  • iOS横屏的深入研究

    iOS横屏模式分2种:跟随系统自动旋转、强制横屏。无论哪种横屏模式,都有2中实现途径:1.重写系统旋转方法。2.对...

  • 屏幕旋转

    屏幕旋转 推荐文档 了解UIWindow——UIWindow实践 iOS屏幕旋转问题总结 IOS:屏幕旋转与变换 ...

  • ios 横屏 旋转 滑动列表问题

    问题 : 解决思路 : 代码

  • iOS屏幕旋转问题 横竖屏处理

    针对上一版本发现还有一些问题 针对优化处理 一 在APP中所需的设置 1.在配置中关闭屏幕方向 将控制权交给代码(...

  • 处理视频旋转逻辑

    解决自动旋转和点击按钮旋转冲突问题(点击按钮旋转回退竖屏后,不能自动旋转) 答:其实只要在点击按钮旋转回竖屏时再加...

  • 【React Native】react-native-orien

    一.问题分析 问题的原因在这篇已经说明:【iOS】当设备方向已经旋转,转屏效果失效的解决方案 二.解决方案 1.修...

网友评论

      本文标题:iOS 旋转屏的问题

      本文链接:https://www.haomeiwen.com/subject/dwyfmftx.html