美文网首页
iOS中实现单个页面支持横竖屏(其他页面只能竖屏)

iOS中实现单个页面支持横竖屏(其他页面只能竖屏)

作者: 安红唯CC | 来源:发表于2018-05-29 14:08 被阅读18次

一直APP都是只支持竖屏的,现在需求是一个界面要支持横屏,于是在网上找到的解决方案,这里记录一哈:

1 首先需要Xcode中选中支持的屏幕方向

image

2 .

在Appdelegate中 .h

@property (nonatomic,assign)NSInteger allowRotate;

.m中

//此方法会在设备横竖屏变化的时候调用
- (UIInterfaceOrientationMask)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window{
     if (_allowRotate == 1) {
        return UIInterfaceOrientationMaskAll;
    }else{
        return (UIInterfaceOrientationMaskPortrait);
    }
}
// 返回是否支持设备自动旋转
- (BOOL)shouldAutorotate
{
    if (_allowRotate == 1) {
        return YES;
    }
    return NO;
}

3 在需要支持横竖屏的controller中:

viewWillApplear 中

//在视图出现的时候,将allowRotate改为1,
 AppDelegate * delegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
delegate.allowRotate = 1;

viewWillDisappear中

//在视图出现的时候,将allowRotate改为0,
 AppDelegate * delegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
   delegate.allowRotate = 0;

写好以上代码之后, 会发现一些问题: 当横屏页面直接点击“返回”按钮退出的时候, 页面依然是横屏, 而我们需要的是仅一个页面可以横屏,测试需要在viewWillDisappear中加入如下代码:

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 = UIInterfaceOrientationPortrait;
        [invocation setArgument:&val atIndex:2];
        [invocation invoke];
    }

此时就可以使app仅有设置页面支持横竖屏了!
此时如果app要求用户在横屏 竖屏的模式下改变UI(横屏与竖屏对应不同的UI), 可以在以下方法中执行

- (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration
{
    // do something before rotation  
  if (toInterfaceOrientation == UIInterfaceOrientationLandscapeLeft || toInterfaceOrientation == UIInterfaceOrientationLandscapeRight) {
        屏幕从竖屏变为横屏时执行
    }else{
        屏幕从横屏变为竖屏时执行
    }
  }
- (void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation
{
    // do something after rotation
}

相关文章

  • iOS中实现单个页面支持横竖屏(其他页面只能竖屏)

    一直APP都是只支持竖屏的,现在需求是一个界面要支持横屏,于是在网上找到的解决方案,这里记录一哈: 1 首先需要X...

  • iOS 应用整体竖屏,单个页面横屏

    iOS 应用整体竖屏,单个页面横屏

  • iOS旋转屏幕设置总结

    iOS开发中,经常会碰到某些页面需要支持横屏显示,某些又仅支持竖屏显示,那怎么样才能比较完美的实现各个界面的横竖屏...

  • iOS: 手机 支持屏幕 方向

    [经验]iOS app整体是竖屏(横屏),某个页面却支持横竖屏](http://blog.csdn.net/hhe...

  • iOS 开发之需要的页面只支持竖屏

    好久没有写过简书了呢,今天就来记录一下项目中遇到的一个小问题:个别页面只支持横屏或是竖屏,其他页面支持横竖屏。参考...

  • iOS实录11:代码处理iOS的横竖屏旋转

    [这是第11篇] 导语: iOS App中大多数页面是只展示竖屏下的效果,但是少部分页面需要支持横竖屏。本文分别介...

  • iOS的横、竖屏和旋转

    前言: iOS App中大多数页面是只展示竖屏下的效果,但是少部分页面需要支持横竖屏。本文分别介绍监听屏幕旋转方向...

  • Unverse通配版本总结

    本项目需求是手机端支持竖屏,个别页面支持横屏,pad端支持横竖屏。 一:对不同端做横竖屏方向的权限限制: 第一种方...

  • iOS 横竖屏总结笔记

    项目APP中总会遇到某些页面需要横屏展示,其他页面默认竖屏展示。所以总结了一套自己使用的横竖屏方法。 首先我查看U...

  • 横竖屏

    横竖屏需求在开发中遇到一个特殊需求,在整个APP的大部分页面都支持竖屏模式,但在某些个别页面,如视频播放的页面或游...

网友评论

      本文标题:iOS中实现单个页面支持横竖屏(其他页面只能竖屏)

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