標籤:技術 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強制橫豎屏轉換