iOS設(shè)置屏幕方向代碼示例
UIDevice+Orientation.h
#import <UIKit/UIKit.h>
NS_ASSUME_NONNULL_BEGIN
@interface UIDevice ()
/** 設(shè)置屏幕方向璃吧,小于iOS16 */
- (void)setOrientation:(UIInterfaceOrientation)orientation API_DEPRECATED("iOS16+使用setOrientationIOS16:", ios(2.0, 16.0));
@end
@interface UIDevice (Orientation)
/** 設(shè)置屏幕方向奇徒,iOS16+ */
- (void)setOrientationIOS16:(UIInterfaceOrientationMask)orientation API_AVAILABLE(ios(16.0));
- (void)setOrientationIOS16:(UIInterfaceOrientationMask)orientation errorHandler:(nullable void (^)(NSError *error))errorHandler API_AVAILABLE(ios(16.0));
@end
NS_ASSUME_NONNULL_END
UIDevice+Orientation.m
#import "UIDevice+Orientation.h"
@implementation UIDevice (Orientation)
- (void)setOrientationIOS16:(UIInterfaceOrientationMask)orientation
{
[self setOrientationIOS16:orientation errorHandler:nil];
}
- (void)setOrientationIOS16:(UIInterfaceOrientationMask)orientation errorHandler:(nullable void (^)(NSError *error))errorHandler
{
if(@available(iOS 16.0,*)){
UIWindowScene *scene = (UIWindowScene *)UIApplication.sharedApplication.connectedScenes.allObjects.firstObject;
UIWindowSceneGeometryPreferencesIOS *geometryPreferences = [[UIWindowSceneGeometryPreferencesIOS alloc] init];
geometryPreferences.interfaceOrientations = orientation;
if(errorHandler){
[scene requestGeometryUpdateWithPreferences:geometryPreferences errorHandler:errorHandler];
}else{
[scene requestGeometryUpdateWithPreferences:geometryPreferences errorHandler:^(NSError * _Nonnull error) {
NSLog(@"屏幕旋轉(zhuǎn)失敗: %@",error);
}];
}
}
}
@end
優(yōu)化版本(推薦使用)
UIDevice+Orientation.h
#import <UIKit/UIKit.h>
NS_ASSUME_NONNULL_BEGIN
/**
用于設(shè)置UI界面方向的自定義枚舉值
*/
typedef NS_ENUM(NSInteger, TKInterfaceOrientation){
TKInterfaceOrientationPortrait,
TKInterfaceOrientationPortraitUpsideDown,
TKInterfaceOrientationLandscapeLeft,
TKInterfaceOrientationLandscapeRight
};
@interface UIDevice (Orientation)
/** 手動(dòng)設(shè)置屏幕方向 */
- (void)setUIOrientation:(TKInterfaceOrientation)orientation;
/** 手動(dòng)設(shè)置屏幕方向阎姥,新增錯(cuò)誤操作回調(diào) */
- (void)setUIOrientation:(TKInterfaceOrientation)orientation errorHandler:(nullable void (^)(NSError *error))errorHandler;
@end
NS_ASSUME_NONNULL_END
UIDevice+Orientation.m
#import "UIDevice+Orientation.h"
@interface UIDevice ()
/**
功能:設(shè)置屏幕方向崇摄,小于iOS16(手動(dòng)旋轉(zhuǎn))
說明: 直接使用擴(kuò)展來訪問是有方法旧噪,即只聲明不實(shí)現(xiàn)赫模。
注意: 如果出現(xiàn)上架錯(cuò)誤請(qǐng)直接使用以前的老方法實(shí)現(xiàn)慎宾。
*/
- (void)setOrientation:(UIInterfaceOrientation)orientation API_DEPRECATED("iOS16+使用setOrientationIOS16:", ios(2.0, 16.0));
@end
@implementation UIDevice (Orientation)
- (void)setUIOrientation:(TKInterfaceOrientation)orientation
{
[self setUIOrientation:orientation errorHandler:nil];
}
- (void)setUIOrientation:(TKInterfaceOrientation)orientation errorHandler:(nullable void (^)(NSError *error))errorHandler
{
NSInteger realOrientation = [self obtainOrientationRealValueWith:orientation];
if(@available(iOS 16.0,*)){
UIWindowScene *scene = (UIWindowScene *)UIApplication.sharedApplication.connectedScenes.allObjects.firstObject;
UIWindowSceneGeometryPreferencesIOS *geometryPreferences = [[UIWindowSceneGeometryPreferencesIOS alloc] init];
geometryPreferences.interfaceOrientations = realOrientation;
if(errorHandler){
[scene requestGeometryUpdateWithPreferences:geometryPreferences errorHandler:errorHandler];
}else{
[scene requestGeometryUpdateWithPreferences:geometryPreferences errorHandler:^(NSError * _Nonnull error) {
NSLog(@"屏幕旋轉(zhuǎn)失敗: %@",error);
}];
}
}else{
[self setOrientation:realOrientation];
}
}
/**
根據(jù)不同的系統(tǒng)版本獲取TKInterfaceOrientation枚舉對(duì)應(yīng)的真實(shí)值
*/
- (NSInteger)obtainOrientationRealValueWith:(TKInterfaceOrientation)orientation
{
NSInteger real = 0;
if(@available(iOS 16.0,*)){
switch (orientation) {
case TKInterfaceOrientationPortrait:
real = UIInterfaceOrientationMaskPortrait;
break;
case TKInterfaceOrientationPortraitUpsideDown:
real = UIInterfaceOrientationMaskPortraitUpsideDown;
break;
case TKInterfaceOrientationLandscapeLeft:
real = UIInterfaceOrientationMaskLandscapeLeft;
break;
case TKInterfaceOrientationLandscapeRight:
real = UIInterfaceOrientationMaskLandscapeRight;
break;
}
}else{
switch (orientation) {
case TKInterfaceOrientationPortrait:
real = UIInterfaceOrientationPortrait;
break;
case TKInterfaceOrientationPortraitUpsideDown:
real = UIInterfaceOrientationPortraitUpsideDown;
break;
case TKInterfaceOrientationLandscapeLeft:
real = UIInterfaceOrientationLandscapeLeft;
break;
case TKInterfaceOrientationLandscapeRight:
real = UIInterfaceOrientationLandscapeRight;
break;
}
}
return real;
}
@end
解釋
擴(kuò)展了一個(gè)沒有實(shí)現(xiàn)的方法:setOrientation:(UIInterfaceOrientation)orientation潜支,來支持iOS16以下的屏幕方向設(shè)置甸赃。
這種寫法就是使用了OC擴(kuò)展的特性來訪問私有方法。
不使用傳統(tǒng)方式調(diào)用其目的就是簡(jiǎn)化代碼冗酿,如果審核不通過就是用老的方式進(jìn)行調(diào)用即可埠对。