iOS強制橫屏或強制豎屏

原文連接 https://www.jianshu.com/p/d6cb54d2eaa1
親測第二種我這邊是闊以滴app

第一種解決方案(不推薦,直接跳過看第二種解決方案):

//強制轉屏
- (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];
    }
}

強制橫屏:

[self interfaceOrientation:UIInterfaceOrientationLandscapeRight];

強制豎屏:

[self interfaceOrientation:UIInterfaceOrientationPortrait];

只在某一個界面提供轉屏的解決方法以下AppDelegate.m下操做

-(UIInterfaceOrientationMask)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window {
    
    NSLog(@"0000000---------%@",NSStringFromClass([[self topViewController] class]));
    if ([NSStringFromClass([[self topViewController] class]) isEqualToString:@"想要提供轉屏的控制器的名字"]) {
      //橫屏
        return UIInterfaceOrientationMaskLandscapeRight;
    }
      //豎屏
    return UIInterfaceOrientationMaskPortrait;
}
//獲取界面最上層的控制器
- (UIViewController*)topViewController {
    return [self topViewControllerWithRootViewController:[UIApplication sharedApplication].keyWindow.rootViewController];
}
//一層一層的進行查找判斷
- (UIViewController*)topViewControllerWithRootViewController:(UIViewController*)rootViewController {
    if ([rootViewController isKindOfClass:[UITabBarController class]]) {
        UITabBarController* tabBarController = (UITabBarController*)rootViewController;
        return [self topViewControllerWithRootViewController:tabBarController.selectedViewController];
    } else if ([rootViewController isKindOfClass:[UINavigationController class]]) {
        UINavigationController* nav = (UINavigationController*)rootViewController;
        return [self topViewControllerWithRootViewController:nav.visibleViewController];
    } else if (rootViewController.presentedViewController) {
        UIViewController* presentedViewController = rootViewController.presentedViewController;
        return [self topViewControllerWithRootViewController:presentedViewController];
    } else {
        return rootViewController;
    }
}

若是你的應用的根控制器是Nav就把下面這段代碼放到Nav根控制器下,若是是TabVC放到TabVC的下面

- (BOOL)shouldAutorotate{
    return YES;
}
- (UIInterfaceOrientationMask)supportedInterfaceOrientations{
    return UIInterfaceOrientationMaskPortrait;
}

而後在你想橫屏的控制器加上這段代碼,基本上橫屏問題就能夠搞定了,前提是你的這個控制器是moda出來的,若是是push的話就要使用上文提到的強制橫豎屏的方法,下面這段代碼是不起做用的

- (BOOL) shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation {
    return (toInterfaceOrientation == UIInterfaceOrientationLandscapeRight);
}

- (UIInterfaceOrientationMask)supportedInterfaceOrientations {
    return UIInterfaceOrientationMaskLandscapeRight;
}

第二種解決方案:

靈活設置橫豎屏,不用區分Push仍是Present,都是能夠設置。atom

第一步:

在AppDelegate.h中添加旋轉屬性

/**
 * 是否容許轉向
 */
@property(nonatomic,assign)BOOL allowRotation;
在AppDelegate.m中添加轉屏的代理方法

- (UIInterfaceOrientationMask)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(nullable UIWindow *)window

{
    
    if (self.allowRotation == YES) {
        //橫屏
        return UIInterfaceOrientationMaskLandscape;
        
    }else{
        //豎屏
        return UIInterfaceOrientationMaskPortrait;
        
    }
    
}

第二步:

設置橫豎屏的核心方法,我是直接把這個方法添加到了UIDevice的分類中,代碼以下:代理

  • UIDevice+TFDevice.h :
#import <UIKit/UIKit.h>
@interface UIDevice (TFDevice)
/**
 * @interfaceOrientation 輸入要強制轉屏的方向
 */
+ (void)switchNewOrientation:(UIInterfaceOrientation)interfaceOrientation;
@end
  • UIDevice+TFDevice.m :
#import "UIDevice+TFDevice.h"

@implementation UIDevice (TFDevice)

+ (void)switchNewOrientation:(UIInterfaceOrientation)interfaceOrientation
{
        
        NSNumber *resetOrientationTarget = [NSNumber numberWithInt:UIInterfaceOrientationUnknown];
        
        [[UIDevice currentDevice] setValue:resetOrientationTarget forKey:@"orientation"];
        
        NSNumber *orientationTarget = [NSNumber numberWithInt:interfaceOrientation];
        
        [[UIDevice currentDevice] setValue:orientationTarget forKey:@"orientation"];
    
}
@end

第三步:

在須要設置橫屏的控制器的ViewDidLoad中添加下面代碼:code

- (void)viewDidLoad {
    [super viewDidLoad];

    AppDelegate * appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
    //容許轉成橫屏
    appDelegate.allowRotation = YES;
    //調用橫屏代碼
    [UIDevice switchNewOrientation:UIInterfaceOrientationLandscapeRight];
}

第四步 (針對Push出的控制器來講):

須要注意的是push過去的時候變成橫屏,pop出去的時候在設置豎屏,此時最好禁用系統的側滑返回手勢。blog

-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    //禁用側滑手勢方法
    self.navigationController.interactivePopGestureRecognizer.enabled = NO;
}
-(void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:animated];
    //禁用側滑手勢方法
    self.navigationController.interactivePopGestureRecognizer.enabled = YES;
}

第五步:

push控制器:get

//點擊導航欄返回按鈕的時候調用,因此Push出的控制器最好禁用側滑手勢:
AppDelegate * appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
appDelegate.allowRotation = NO;//關閉橫屏僅容許豎屏
//切換到豎屏
[UIDevice switchNewOrientation:UIInterfaceOrientationPortrait];
   
[self.navigationController popViewControllerAnimated:YES];

present控制器:it

AppDelegate * appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
appDelegate.allowRotation = NO;//關閉橫屏僅容許豎屏
//切換到豎屏
[UIDevice switchNewOrientation:UIInterfaceOrientationPortrait];
    
[self dismissViewControllerAnimated:YES completion:nil];

第六步: 上圖

相關文章
相關標籤/搜索