iOS 控制每个vc横竖屏

此处引用的是ZFPlayer的里面的一个旋转控制类,此处手动感谢一下ZFPlayer的作者,受小弟一拜,感谢大神!!!

一、 思想:
  • 由于横竖屏的最终确定是由最跟控制器决定的,然而一般的APP结构就是uitablebarcontroller+uinavigationController+uiviewController,所以可以根据以上结构然后利用系统方法让最终决定横竖屏的地方转移到每个vc里面
二、 具体方法:

1、首先写一个UIViewController的分类,在里面把传递到vc的代码写好,也就是uitablebarcontroller+uinavigationController里面的系统方法重写一下

#import <UIKit/UIKit.h>
#import <objc/runtime.h>

@implementation UITabBarController (ZFPlayerRotation)

+ (void)load {
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        SEL selectors[] = {
            @selector(selectedIndex)
        };
        
        for (NSUInteger index = 0; index < sizeof(selectors) / sizeof(SEL); ++index) {
            SEL originalSelector = selectors[index];
            SEL swizzledSelector = NSSelectorFromString([@"zf_" stringByAppendingString:NSStringFromSelector(originalSelector)]);
            Method originalMethod = class_getInstanceMethod(self, originalSelector);
            Method swizzledMethod = class_getInstanceMethod(self, swizzledSelector);
            if (class_addMethod(self, originalSelector, method_getImplementation(swizzledMethod), method_getTypeEncoding(swizzledMethod))) {
                class_replaceMethod(self, swizzledSelector, method_getImplementation(originalMethod), method_getTypeEncoding(originalMethod));
            } else {
                method_exchangeImplementations(originalMethod, swizzledMethod);
            }
        }
    });
}

- (NSInteger)zf_selectedIndex {
    NSInteger index = [self zf_selectedIndex];
    if (index > self.viewControllers.count) return 0;
    return index;
}

/**
 * If the root view of the window is a UINavigationController, you call this Category first, and then UIViewController called.
 * All you need to do is revisit the following three methods on a page that supports directions other than portrait.
 */

// Whether automatic screen rotation is supported.
- (BOOL)shouldAutorotate {
    UIViewController *vc = self.viewControllers[self.selectedIndex];
    if ([vc isKindOfClass:[UINavigationController class]]) {
        UINavigationController *nav = (UINavigationController *)vc;
        return [nav.topViewController shouldAutorotate];
    } else {
        return [vc shouldAutorotate];
    }
}

// Which screen directions are supported.
- (UIInterfaceOrientationMask)supportedInterfaceOrientations {
    UIViewController *vc = self.viewControllers[self.selectedIndex];
    if ([vc isKindOfClass:[UINavigationController class]]) {
        UINavigationController *nav = (UINavigationController *)vc;
        return [nav.topViewController supportedInterfaceOrientations];
    } else {
        return [vc supportedInterfaceOrientations];
    }
}

// The default screen direction (the current ViewController must be represented by a modal UIViewController (which is not valid with modal navigation) to call this method).
- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation {
    UIViewController *vc = self.viewControllers[self.selectedIndex];
    if ([vc isKindOfClass:[UINavigationController class]]) {
        UINavigationController *nav = (UINavigationController *)vc;
        return [nav.topViewController preferredInterfaceOrientationForPresentation];
    } else {
        return [vc preferredInterfaceOrientationForPresentation];
    }
}

@end

@implementation UINavigationController (ZFPlayerRotation)

/**
 * If the root view of the window is a UINavigationController, you call this Category first, and then UIViewController called.
 * All you need to do is revisit the following three methods on a page that supports directions other than portrait.
 */

// Whether automatic screen rotation is supported
- (BOOL)shouldAutorotate {
    return [self.topViewController shouldAutorotate];
}

// Which screen directions are supported
- (UIInterfaceOrientationMask)supportedInterfaceOrientations {
    return [self.topViewController supportedInterfaceOrientations];
}

// The default screen direction (the current ViewController must be represented by a modal UIViewController (which is not valid with modal navigation) to call this method).
- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation {
    return [self.topViewController preferredInterfaceOrientationForPresentation];
}

- (UIViewController *)childViewControllerForStatusBarStyle {
    return self.topViewController;
}

- (UIViewController *)childViewControllerForStatusBarHidden {
    return self.topViewController;
}

@end

2、然后在每个vc里面就可以使用下面的代码单独设置强制横、竖屏,或者横竖屏随意切换

- (UIStatusBarStyle)preferredStatusBarStyle {
    if (self.player.isFullScreen) {
        return UIStatusBarStyleLightContent;
    }
    return UIStatusBarStyleDefault;
}

- (BOOL)prefersStatusBarHidden {
    return self.player.isStatusBarHidden;
}

- (UIStatusBarAnimation)preferredStatusBarUpdateAnimation {
    return UIStatusBarAnimationSlide;
}

- (BOOL)shouldAutorotate {
    return self.player.shouldAutorotate;
}

- (UIInterfaceOrientationMask)supportedInterfaceOrientations {
    return UIInterfaceOrientationMaskLandscape;
}

- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation {
    return UIInterfaceOrientationLandscapeRight;
}

相关推荐

  1. iOS 控制每个vc横竖

    2024-04-20 11:02:09       18 阅读
  2. iOS 横竖的正确打开姿势

    2024-04-20 11:02:09       16 阅读
  3. iOS 让APP支持横竖

    2024-04-20 11:02:09       7 阅读
  4. uniapp横竖配置

    2024-04-20 11:02:09       9 阅读
  5. HarmonyOS ArkTS 横竖设置

    2024-04-20 11:02:09       20 阅读
  6. Android 横竖切换 窗口全

    2024-04-20 11:02:09       39 阅读
  7. Android : ViewModel_解决textView 横竖数据丢失

    2024-04-20 11:02:09       38 阅读
  8. 适配方案(vwvh

    2024-04-20 11:02:09       32 阅读

最近更新

  1. TCP协议是安全的吗?

    2024-04-20 11:02:09       18 阅读
  2. 阿里云服务器执行yum,一直下载docker-ce-stable失败

    2024-04-20 11:02:09       19 阅读
  3. 【Python教程】压缩PDF文件大小

    2024-04-20 11:02:09       18 阅读
  4. 通过文章id递归查询所有评论(xml)

    2024-04-20 11:02:09       20 阅读

热门阅读

  1. Rust 语言使用 SQLite 数据库

    2024-04-20 11:02:09       13 阅读
  2. PgSQL的登录相关(Ubuntu22.04)

    2024-04-20 11:02:09       15 阅读
  3. es6 常用的object归纳总结和部分数组纠结总结

    2024-04-20 11:02:09       13 阅读
  4. Ribbon负载均衡

    2024-04-20 11:02:09       12 阅读
  5. 【Node.js】child_process 子进程

    2024-04-20 11:02:09       14 阅读
  6. tensorflow list_files需要注意的点

    2024-04-20 11:02:09       12 阅读
  7. Create2024百度AI开发者大会记录

    2024-04-20 11:02:09       14 阅读
  8. Remote access minikube cluster远程访问minikube k8s集群

    2024-04-20 11:02:09       13 阅读
  9. 学习基于pytorch的VGG图像分类 day4

    2024-04-20 11:02:09       12 阅读
  10. 独孤思维:副业赚钱的课程被盗了

    2024-04-20 11:02:09       14 阅读
  11. 1.8、数位DP(算法提高课)

    2024-04-20 11:02:09       12 阅读