Xcode9 2018年最新关于app编写中,大部分界面强制竖屏,个别界面使用横屏完美解决

第一步:


8180BBD0-9D78-4110-980A-F1D0F2651D27.png

第二步:
在AppDelegate里面添加方法2个

  • (UIViewController *)getViewController

{

UIViewController *appRootVC = [UIApplication sharedApplication].keyWindow.rootViewController;

UIViewController *topVC = appRootVC;

if (topVC.presentedViewController) {
    
    topVC = topVC.presentedViewController;
    
}

return topVC;

}

//锁定用户手机APP为竖屏模式

  • (NSUInteger)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window
    {
    //这里是你需要横屏的部分控制器 ,我这里是3个控制器必须为横屏,其余全部是横屏
    UIViewController * vc = [self getViewController];
    if ([vc isKindOfClass:[DYMarkingVC class]]|| [vc isKindOfClass:[DYMarkingQuestionVC class]] || [vc isKindOfClass:[DYMarkingArbitrationVC class]] ) {
    return UIInterfaceOrientationMaskAll;
    }else{
    return UIInterfaceOrientationMaskPortrait;
    }

}

第三步:
具体需要横屏的控制器添加下面方法

pragma mark 强制横屏(针对present方式)

  • (BOOL) shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation {
    return (UIInterfaceOrientationLandscapeRight | UIInterfaceOrientationLandscapeLeft);
    }

  • (UIInterfaceOrientationMask)supportedInterfaceOrientations {
    return UIInterfaceOrientationMaskLandscapeRight | UIInterfaceOrientationMaskLandscapeLeft;
    }

//必须有
-(UIInterfaceOrientation)preferredInterfaceOrientationForPresentation{
return UIInterfaceOrientationLandscapeRight;
}

第四步:
在竖屏A prensent到横屏B中,所有A都要添加代码
//强制转屏

  • (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)viewWillAppear:(BOOL)animated{
[super viewWillAppear:YES];

[self interfaceOrientation:UIInterfaceOrientationPortrait];

}

感谢兄弟技术支持:请输入账号名

灰客原创,转帖请注明出处,希望有大神能斧正!

你可能感兴趣的:(Xcode9 2018年最新关于app编写中,大部分界面强制竖屏,个别界面使用横屏完美解决)