美文网首页
iOS技巧篇:强制特定页面的横竖屏

iOS技巧篇:强制特定页面的横竖屏

作者: younger_times | 来源:发表于2018-03-02 14:39 被阅读78次

    功能概要: 支持部分页面可以旋转,部分页面不支持旋转。

    在项目中的TARGETS --> Develoyment info --> Device Orientation 支持4种旋转方式。

    1. portrait 本身的竖屏
    2. upside down 竖屏反向
    3. landscape left
    4. landscape right

    这种设置是全局化的,不能针对页面。

    代码设置方向有两个方法

    1. 设置

    是否支持自动旋转

    - (BOOL)shouldAutorotate{  
         returnYES;
    }
    

    支持自动旋转,那么支持旋转方向

    - (NSUInteger)supportedInterfaceOrientations
    {
    returnUIInterfaceOrientationMaskAll;
    }
    

    When the user changes the device orientation, the system calls this method on the root view controller or the topmost presented view controller that flls the window. If the view controller supports the new orientation, the window and view controller are rotated to the new orientation. This method is only called if the view controller's shouldAutorotate method returns YES.

    但官方说明了, on the root view controller,不能分别在A,B 控制器设置,必须设置在base控制器上面。如果你是Tab,那么就在Tab控制器中设置。

    我将YKBaseNavigationC设置成root控制器,那么上面两个方法在这个类中设置

    - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
        self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
        self.window.backgroundColor = [UIColor whiteColor];
    
        YKBaseNavigationC *nav = [[YKBaseNavigationC alloc]initWithRootViewController:[[YKMainVC alloc]init]];
        self.window.rootViewController = nav;
        [self.window makeKeyAndVisible];
        return YES;
    }
    

    YKBaseNavigationC中

    -(instancetype)initWithRootViewController:(UIViewController *)rootViewController{
        self = [super initWithRootViewController:rootViewController];
        if (self) {
    
        }
        return self;
    }
    
    - (BOOL)shouldAutorotate{
        return [self.viewControllers.lastObject shouldAutorotate];
    }
    
    - (UIInterfaceOrientationMask)supportedInterfaceOrientations{
        return [self.viewControllers.lastObject supportedInterfaceOrientations];
    }
    
    - (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation{
        return [self.viewControllers.lastObject preferredInterfaceOrientationForPresentation];
    }
    

    那么任何被push,pop的VC,都可以支持方向变化

    例子

    我在A控制器中添加下面方法

    -(UIInterfaceOrientationMask)supportedInterfaceOrientations {
        return UIInterfaceOrientationMaskLandscapeRight; 
    }
    
    //设置不允许旋转
    - (BOOL) shouldAutorotate {
        return NO;
    }
    

    2. 强制旋转方向

    接上面的话,任何push,pop的vc,都支持,那么我将push YKExpenseTableVC

    -(void)viewWillAppear:(BOOL)animated{
        [super viewWillAppear:animated];
        [self.navigationController setNavigationBarHidden:NO];
    
        //设置方向
        [[UIDevice currentDevice] setValue:[NSNumber numberWithInteger:UIInterfaceOrientationLandscapeRight] forKey:@"orientation"];
    }
    

    Note

    有时候不会执行,我不知道为什么

    3. 屏幕旋转时监听方向变化

    我们大多数是设置自动布局时,都采用下面的宏,屏幕旋转产生了数值变化,使得布局瞬间凌乱。这时需要监听屏幕方向来刷新布局。

    #define SCREEN_WIDTH [UIScreen mainScreen].bounds.size.width
    #define SCREEN_HEIGHT [UIScreen mainScreen].bounds.size.height
    
    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(changeRotate:) name:UIApplicationDidChangeStatusBarFrameNotification object:nil];
    
    - (void)changeRotate:(NSNotification*)noti {
        if ([[UIDevice currentDevice] orientation] == UIInterfaceOrientationPortrait
            || [[UIDevice currentDevice] orientation] == UIInterfaceOrientationPortraitUpsideDown) {
            //竖屏
            NSLog(@"竖屏");
        } else {
            //横屏
             NSLog(@"横屏");
        }
    }
    

    相关文章

      网友评论

          本文标题:iOS技巧篇:强制特定页面的横竖屏

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