第一步:

8180BBD0-9D78-4110-980A-F1D0F2651D27.png
第二步:
在AppDelegate里面添加方法2個(gè)
- (UIViewController *)getViewController
{
UIViewController *appRootVC = [UIApplication sharedApplication].keyWindow.rootViewController;
UIViewController *topVC = appRootVC;
if (topVC.presentedViewController) {
topVC = topVC.presentedViewController;
}
return topVC;
}
//鎖定用戶手機(jī)APP為豎屏模式
- (NSUInteger)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window
{
//這里是你需要橫屏的部分控制器 ,我這里是3個(gè)控制器必須為橫屏,其余全部是橫屏
UIViewController * vc = [self getViewController];
if ([vc isKindOfClass:[DYMarkingVC class]]|| [vc isKindOfClass:[DYMarkingQuestionVC class]] || [vc isKindOfClass:[DYMarkingArbitrationVC class]] ) {
return UIInterfaceOrientationMaskAll;
}else{
return UIInterfaceOrientationMaskPortrait;
}
}
第三步:
具體需要橫屏的控制器添加下面方法
pragma mark 強(qiáng)制橫屏(針對(duì)present方式)
(BOOL) shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation {
return (UIInterfaceOrientationLandscapeRight | UIInterfaceOrientationLandscapeLeft);
}(UIInterfaceOrientationMask)supportedInterfaceOrientations {
return UIInterfaceOrientationMaskLandscapeRight | UIInterfaceOrientationMaskLandscapeLeft;
}
//必須有
-(UIInterfaceOrientation)preferredInterfaceOrientationForPresentation{
return UIInterfaceOrientationLandscapeRight;
}
第四步:
在豎屏A prensent到橫屏B中,所有A都要添加代碼
//強(qiáng)制轉(zhuǎn)屏
- (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開始是因?yàn)? 1 兩個(gè)參數(shù)已經(jīng)被selector和target占用
[invocation setArgument:&val atIndex:2];
[invocation invoke];
}
}
-(void)viewWillAppear:(BOOL)animated{
[super viewWillAppear:YES];
[self interfaceOrientation:UIInterfaceOrientationPortrait];
}
感謝兄弟技術(shù)支持:請(qǐng)輸入賬號(hào)名
灰客原創(chuàng),轉(zhuǎn)帖請(qǐng)注明出處,希望有大神能斧正!