Xcode9 2018年最新關(guān)于app編寫中,大部分界面強(qiáng)制豎屏,個(gè)別界面使用橫屏完美解決

第一步:


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)注明出處,希望有大神能斧正!

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容