美文网首页iOS项目框架搭建iOS Developer
iOS 个别界面选择性横屏的解决办法

iOS 个别界面选择性横屏的解决办法

作者: Kean_Qi | 来源:发表于2016-11-15 22:30 被阅读56次
    在 APPDelegate.h 文件中增加属性:是否支持横屏
    /***  是否允许横屏的标记 */
    @property (nonatomic,assign)BOOL allowRotation;
    
    在 APPDelegate.m 文件中增加方法,控制全部不支持横屏
    - (UIInterfaceOrientationMask)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window {
        if (self.allowRotation) {
            return  UIInterfaceOrientationMaskAllButUpsideDown;
        }
        return UIInterfaceOrientationMaskPortrait;
    }
    
    这样在其他界面想要横屏的时候,我们只要控制 allowRotation 这个属性就可以控制其他界面进行横屏了。
    //需在上面#import "AppDelegate.h"
    AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
    appDelegate.allowRotation = YES;
    //不让横屏的时候 appDelegate.allowRotation = NO;即可
    
    需要横屏时,进入界面时
    - (void)viewWillAppear:(BOOL)animated {
        [super viewWillAppear:animated];
    //如果有导航设置的话 先设置导航
        [self.navigationController setNavigationBarHidden:YES animated:YES];
        [self begainFullScreen];
    }
    
    -(void)begainFullScreen
    {
        AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
        appDelegate.allowRotation = YES;
    }
    
    
    需要横屏时,退出界面时
    - (void)viewWillDisappear:(BOOL)animated {
        [super viewWillDisappear:animated];
    //如果有导航设置的话 先设置导航
        //[self.navigationController setNavigationBarHidden:NO animated:YES];
        //不让横屏的时候
        [self endFullScreen];
    }
    
    // 退出全屏
    -(void)endFullScreen
    {
        AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
        appDelegate.allowRotation = NO;
    
        //强制归正:
        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];
        }
    }
    
    
    界面横屏 所以这里可以使用 通知,就可以解决
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(begainFullScreen) name:UIWindowDidBecomeVisibleNotification object:nil];//进入全屏
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(endFullScreen) name:UIWindowDidBecomeHiddenNotification object:nil];//退出全屏
    在退出全屏时,增加逻辑让其强制编程竖屏,这样当退出时就会自动变成竖屏了。
    ```
    
    
    
    
    
    ```
    // 进入全屏
    -(void)begainFullScreen
    {
        AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
        appDelegate.allowRotation = YES;
    }
    ```
    ```
    // 退出全屏
    -(void)endFullScreen
    {
        AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
        appDelegate.allowRotation = NO;
    
        //强制归正:
        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];
        }
    }
    ```
    一进入页面就横屏:
    ```
    NSNumber *orientationTarget = [NSNumber numberWithInt:UIInterfaceOrientationLandscapeLeft];
        [[UIDevice currentDevice] setValue:orientationTarget forKey:@"orientation"];
    ```

    相关文章

      网友评论

        本文标题:iOS 个别界面选择性横屏的解决办法

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