iOS強制橫豎屏轉換

來源:互聯網
上載者:User

標籤:技術   ret   iss   value   .com   lan   ffffff   with   eset   

https://www.jianshu.com/p/d6cb54d2eaa1  這篇文章給出的方案是可行的。

 

經測試,想要第一個介面強制橫屏,第二個介面強制豎屏, dismiss掉之後回到第一個介面依然強制橫屏。使用者的手機裝置禁止旋轉按鈕不開啟狀態下,旋轉手機螢幕能依然強制橫屏或者豎屏。

第一個介面強制橫屏 在工程設定裡

如果是要第一個介面強制豎屏 那麼設定為

 

AppDelegate裡的系統方法為:

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;

        

    }

    

}

 

在第一個介面的控制器裡添加代碼

#import "AppDelegate.h"

#import "UIDevice+TFDevice.h"

-(void)viewWillAppear:(BOOL)animated{

    [super viewWillAppear:animated];

     NSLog(@"%s",__func__);

    AppDelegate * appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;

    //允許轉成橫屏

    

    appDelegate.allowRotation = YES;

    //調用橫屏代碼

    

[UIDevice switchNewOrientation:UIInterfaceOrientationLandscapeLeft];

    

}

 

在第二個介面的控制器裡

添加代碼

 

#import "AppDelegate.h"

#import "UIDevice+TFDevice.h"

 

- (void)viewDidLoad {

    [super viewDidLoad];

    NSLog(@"%s",__func__);

    AppDelegate * appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;

    //允許轉成豎屏

   

    appDelegate.allowRotation = NO;

    //調用豎屏代碼

 [UIDevice switchNewOrientation:UIInterfaceOrientationPortrait];

  

}

 

另外對UIDevice裝置做一個分類擴充

@interface UIDevice (TFDevice)

/**

 * @interfaceOrientation 輸入要強制轉屏的方向

 */

+ (void)switchNewOrientation:(UIInterfaceOrientation)interfaceOrientation;

@end

 

#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

iOS強制橫豎屏轉換

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.