IOS横竖屏显示

    xiaoxiao2021-12-03  20

    由于IOS的横竖屏设置只有在根视图才会有效果。所以 想了一个方法就是找到根视图,添加一个类函数,在任何地方都去设置横竖屏的参数。

    下边的例子是指定窗口横屏,其他竖屏显示。

    1.找到根视图类,添加属性。我的demo工程根视图是 MyTabBarViewController 所以就在MyTabBarViewController中添加

    static UIInterfaceOrientationMask orientation =UIInterfaceOrientationMaskPortrait;

    添加类函数

    +(void)setViewOrientation:(UIInterfaceOrientationMask)orientations{

     

        orientation = orientations;

    }

    重载函数

    // 指定支持的旋转情况,须在最底层的viewcontroller实现

    - (UIInterfaceOrientationMask)supportedInterfaceOrientations

    {

        returnorientation;

    }

    2.在要单独设置横屏的界面添加代码

     [MyTabBarViewControllersetViewOrientation:UIInterfaceOrientationMaskLandscapeRight |UIInterfaceOrientationMaskLandscapeLeft];

     由于设备当时可能是竖屏显示效果,如果没有手机方向改变的事件触发,就算是设置了横竖屏参数也不会横屏显示需要添加代码,去横屏显示

     

    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 = UIInterfaceOrientationLandscapeRight; [invocation setArgument:&val atIndex:2]; [invocation invoke]; }  在该视图消失的时候,需要改回竖屏效果。同理需要添加代码

      

    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]; } 综上所述:

    - (void)viewDidLoad { [super viewDidLoad]; [self.view setBackgroundColor:[UIColor whiteColor]]; [MyTabBarViewController setViewOrientation:UIInterfaceOrientationMaskLandscapeRight | UIInterfaceOrientationMaskLandscapeLeft]; 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 = UIInterfaceOrientationLandscapeRight; [invocation setArgument:&val atIndex:2]; [invocation invoke]; } // Do any additional setup after loading the view. } -(void)viewWillDisappear:(BOOL)animated{ [MyTabBarViewController setViewOrientation:UIInterfaceOrientationMaskPortrait]; 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]; } }

    Demo源码

    转载请注明原文地址: https://ju.6miu.com/read-679881.html

    最新回复(0)