iOS - 横竖屏相关篇

以前都是将知识点写在 《iOS - 小常识知识点这篇文章》 ,但是后来累积的太多,所以还是决定抽时间按功能模块抽分出来!

   0.点项目 - Targets - General - Deployment Info ,如图
C70563D6-8465-4F95-A2BA-B26519663417.png

第一、默认所有竖屏之强制横屏版 -- UIDevice 和 Animation 版 (无需解锁即可强制横竖屏)

    1.项目名称 - TARGETS - General - Deployment Info - Device Orientation , 勾选Portrait !

    2.创建按钮,并实现点击按钮,强制横屏

            UIView *navView = [[UIView alloc] initWithFrame:CGRectMake(10, 64+10, WIDTH-20, 200)];
            navView.backgroundColor = [UIColor clearColor];
            [self.view addSubview:navView];
            UIButton *testBtn = [UIButton buttonWithType:UIButtonTypeCustom];
            testBtn.backgroundColor = SLIVERYCOLOR;
            testBtn.frame = CGRectMake(0, 170, 50, 30);
            [navView addSubview:testBtn];
            [testBtn addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];    

            - (void)btnClick:(UIButton *)sender{
                sender.selected = !sender.selected;
                [[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications];
                [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(deviceOrientationDidChange) name:UIDeviceOrientationDidChangeNotification object:nil];
                if (sender.selected) {
                    [self interfaceOrientation:UIInterfaceOrientationLandscapeRight];
                }else{
                    [self interfaceOrientation:UIInterfaceOrientationPortrait];
                }
            }
            
            - (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];
                }
            }
            
            - (void)deviceOrientationDidChange
            {
                NSLog(@"NAV deviceOrientationDidChange:%ld",(long)[UIDevice currentDevice].orientation);
                if([UIDevice currentDevice].orientation == UIDeviceOrientationPortrait) {
                    [[UIApplication sharedApplication] setStatusBarOrientation:UIInterfaceOrientationPortrait animated:YES];
                    [self orientationChange:NO];
                    //注意: UIDeviceOrientationLandscapeLeft 与 UIInterfaceOrientationLandscapeRight
                } else if ([UIDevice currentDevice].orientation == UIDeviceOrientationLandscapeLeft) {
                    [[UIApplication sharedApplication] setStatusBarOrientation:UIInterfaceOrientationLandscapeRight animated:YES];
                    [self orientationChange:YES];
                }
            }
            
            - (void)orientationChange:(BOOL)landscapeRight
            {
                if (landscapeRight) {
                    [UIView animateWithDuration:0.2f animations:^{
                        self.view.transform = CGAffineTransformMakeRotation(M_PI_2);
                        self.view.bounds = CGRectMake(0, 0, WIDTH, HEIGHT);
                    }];
                } else {
                    [UIView animateWithDuration:0.2f animations:^{
                        self.view.transform = CGAffineTransformMakeRotation(0);
                        self.view.bounds = CGRectMake(0, 0, WIDTH, HEIGHT);
                    }];
                }
            }
            
            - (BOOL)shouldAutorotate
            {
                return NO;
            }

    3.如果该横屏控制器为根控制器,此步忽视!如果该横屏控制器为导航控制器下的子控制器,或者是tabbar控制器下的导航控制器下的子控制器,则需要重写 父控制器的 shouldAutorotate 方法直到跟控制器未知!

   #pragma mark - 注意点:必须重写父类控制器直至根控制器!在重写方法的 shouldAutorotate 方法里面传入需要横屏的控制器!

    #pragma mark - 旋转相关 (tabbar控制器下的子控制器需要旋转需要重写此方法,且需要旋转哪个控制器就传入哪个)
    - (BOOL)shouldAutorotate
    {
        //在viewControllers中返回需要改变的NavigationController
        //由于此处项目中只有一个基类导航控制器,所以五个控制器名称相同,就取 要横屏的第四个导航控制器 - self.viewControllers[3]
        return [self.viewControllers[3] shouldAutorotate];
    }

    #pragma mark - 旋转相关 (导航控制器下的子控制器需要旋转需要重写此方法,且需要旋转哪个控制器就传入哪个)

    - (BOOL)shouldAutorotate
    {
        //在viewControllers中返回需要改变的viewController
        for (id obj in self.viewControllers) {
            if ([obj isKindOfClass:[MSUPlayerController class]]) {
                return [obj shouldAutorotate];
            }
        }
        return 0;
    }

第二、默认所有竖屏之强制横屏版 -- AppDelegate BOOL版 (需手动解锁锁屏按钮方可横竖屏)

    0.项目名称 - TARGETS - General - Deployment Info - Device Orientation , 勾选Portrait !

    1.在 appdelegate 头文件中添加属性

        /// 判断是否横竖屏
        @property (nonatomic,assign)BOOL allowRotation;

    2.在 appdelegate.m 文件中实现方法

        /* 横竖屏 */
        - (UIInterfaceOrientationMask)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window {
            if (self.allowRotation) {
                return  UIInterfaceOrientationMaskAllButUpsideDown;
            }
            return UIInterfaceOrientationMaskPortrait;
        }

    3.在需要横屏的文件中,导入头文件 #import "AppDelegate.h" ,添加如此代码

        /// 横竖屏相关
        AppDelegate *app =(AppDelegate *)[[UIApplication sharedApplication] delegate];
        app.allowRotation = YES;

第三、UI适配版

1.控制器中横竖屏如何分写

    1).在控制器中的  viewWillLayoutSubviews 中分横竖屏情况写控件 frame

        - (void)viewWillLayoutSubviews {
                [super viewWillLayoutSubviews];
                // 通过状态栏电池图标来判断屏幕方向
                if ([UIApplication sharedApplication].statusBarOrientation == UIInterfaceOrientationPortrait) {
                    // 竖屏 balabala
                } else {
                    // 横屏 balabala
                }
        }

    2).在控制器中的 viewWillTransitionToSize 中分横竖屏情况写控件 frame
        - (void)viewWillTransitionToSize:(CGSize)size withTransitionCoordinator:(id<UIViewControllerTransitionCoordinator>)coordinator {
            
            if (size.width > size.height) { // 横屏
                // 横屏布局 balabala
            } else {
                // 竖屏布局 balabala
            }
        }

2.UIView 中横竖屏如何分写

    - (void)layoutSubviews{
        [super layoutSubviews];
        if ([UIApplication sharedApplication].statusBarOrientation == UIInterfaceOrientationPortrait) {
            // 竖屏 balabala
        } else {
            // 横屏 balabala
        }
    }
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 1.监听屏幕旋转方向 在处理iOS横竖屏时,经常会和UIDeviceOrientation、UIInterface...
    彬至睢阳阅读 2,555评论 1 6
  • iOS 中横竖屏切换的功能,在开发iOS app中总能遇到。以前看过几次,感觉简单,但是没有敲过代码实现,最近又碰...
    零度_不结冰阅读 2,238评论 0 0
  • [这是第11篇] 导语: iOS App中大多数页面是只展示竖屏下的效果,但是少部分页面需要支持横竖屏。本文分别介...
    南华coder阅读 14,537评论 18 93
  • 一年多了,一五年年尾我第一次和红月老师见面,参加了北京的玛雅历学习班,到今年二月中旬的上海分享会,这一年多的等待和...
    林墨KELLY阅读 649评论 1 3
  • 2017年7月3日 晴 吃了一半的桃子 也发黄了 一半的粗燥右手 麻木静止了 写了一半的日记 内容干枯了 现在意识...
    鲜栗子阅读 198评论 1 2