項(xiàng)目中針對(duì)某一個(gè) View
需要進(jìn)行橫屏,在 iOS16
之前的方式大部分都是采取設(shè)置設(shè)備的方向來實(shí)現(xiàn)的,但是在 iOS16
開始這種方式已經(jīng)無效了,如果使用設(shè)置設(shè)備方向來實(shí)現(xiàn)橫豎屏切換,在 Xcode
的控制臺(tái)中會(huì)輸出以下信息:
[Orientation] BUG IN CLIENT OF UIKIT: Setting UIDevice.orientation is not supported. Please use UIWindowScene.requestGeometryUpdate(_:)
所以在 iOS16
開始如果要實(shí)現(xiàn)橫豎屏切換,需要使用 UIWindowScene
的方式進(jìn)行
iOS16 之前實(shí)現(xiàn)橫豎屏切換方式
AppDelegate
在 AppDelegate
的 .h 文件中添加一個(gè)變量來記錄是否需要進(jìn)行橫豎屏切換
@property (nonatomic, assign, getter=isLaunchScreen) BOOL launchScreen; /**< 是否是橫屏 */
在 AppDelegate
的 .m 文件中重寫 launchScreen
的 setter
方法
- (void)setLaunchScreen:(BOOL)launchScreen {
_launchScreen = launchScreen;
[self application:[UIApplication sharedApplication] supportedInterfaceOrientationsForWindow:nil];
}
并且實(shí)現(xiàn) UIApplicationDelegate
的 application:supportedInterfaceOrientationsForWindow:
方法
- (UIInterfaceOrientationMask)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window {
if (self.isLaunchScreen) {
// 只支持橫屏,并且 Home 按鍵在右邊
return UIInterfaceOrientationMaskLandscapeRight;
}
// 只支持豎屏
return UIInterfaceOrientationMaskPortrait;
}
在需要實(shí)現(xiàn)橫豎屏切換的 View
接下來在需要切換橫豎屏的 View
中增加以下方法,就能在 iOS16
之前實(shí)現(xiàn)該功能
/// 切換設(shè)備方向
/// - Parameter isLaunchScreen: 是否是全屏
- (void)p_switchOrientationWithLaunchScreen:(BOOL)isLaunchScreen {
AppDelegate *appdelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
if (isLaunchScreen) {
// 全屏操作
appdelegate.launchScreen = YES;
} else {
// 退出全屏操作
appdelegate.launchScreen = NO;
}
// 設(shè)置設(shè)備的方向
[self p_swichToNewOrientation:isLaunchScreen ? UIInterfaceOrientationLandscapeRight : UIInterfaceOrientationPortrait];
}
/// iOS16 之前進(jìn)行橫豎屏切換方式
/// - Parameter interfaceOrientation: 需要切換的方向
- (void)p_swichToNewOrientation:(UIInterfaceOrientation)interfaceOrientation {
NSNumber *orientationTarget = [NSNumber numberWithInteger:interfaceOrientation];
[[UIDevice currentDevice] setValue:orientationTarget forKey:@"orientation"];
}
經(jīng)過以上代碼,就能實(shí)現(xiàn)在 iOS16
之前的設(shè)備上進(jìn)行橫豎屏切換,下面開始適配 iOS16
的橫豎屏切換
iOS16 實(shí)現(xiàn)橫豎屏切換
AppDelegate
跟 iOS16
之前方式一樣,需要設(shè)置 launchScreen
標(biāo)志變量,重寫 launchScreen
的 setter
方法,實(shí)現(xiàn) UIApplicationDelegate
的 application:supportedInterfaceOrientationsForWindow:
方法
在需要實(shí)現(xiàn)橫豎屏切換的 View
在 p_switchOrientationWithLaunchScreen:
方法中增加 iOS16
適配
/// 切換設(shè)備方向
/// - Parameter isLaunchScreen: 是否是全屏
- (void)p_switchOrientationWithLaunchScreen:(BOOL)isLaunchScreen {
AppDelegate *appdelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
if (isLaunchScreen) {
// 全屏操作
appdelegate.launchScreen = YES;
} else {
// 退出全屏操作
appdelegate.launchScreen = NO;
}
if (@available(iOS 16.0, *)) {
// setNeedsUpdateOfSupportedInterfaceOrientations 方法是 UIViewController 的方法,所以這個(gè)操作最好是放在控制器中去操作
[self setNeedsUpdateOfSupportedInterfaceOrientations];
NSArray *array = [[[UIApplication sharedApplication] connectedScenes] allObjects];
UIWindowScene *scene = [array firstObject];
// 屏幕方向
UIInterfaceOrientationMask orientation = isLaunchScreen ? UIInterfaceOrientationMaskLandscapeRight : UIInterfaceOrientationMaskPortrait;
UIWindowSceneGeometryPreferencesIOS *geometryPreferencesIOS = [[UIWindowSceneGeometryPreferencesIOS alloc] initWithInterfaceOrientations:orientation];
// 開始切換
[scene requestGeometryUpdateWithPreferences:geometryPreferencesIOS errorHandler:^(NSError * _Nonnull error) {
NSLog(@"強(qiáng)制%@錯(cuò)誤:%@", isLaunchScreen ? @"橫屏" : @"豎屏", error);
}];
} else {
[self p_swichToNewOrientation:isLaunchScreen ? UIInterfaceOrientationLandscapeRight : UIInterfaceOrientationPortrait];
}
}
如果 Xcode
沒有更新到 14,需要采用委婉的方式進(jìn)行實(shí)現(xiàn)
/// 切換設(shè)備方向
/// - Parameter isLaunchScreen: 是否是全屏
- (void)p_switchOrientationWithLaunchScreen:(BOOL)isLaunchScreen {
AppDelegate *appdelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
if (isLaunchScreen) {
// 全屏操作
appdelegate.launchScreen = YES;
} else {
// 退出全屏操作
appdelegate.launchScreen = NO;
}
if (@available(iOS 16.0, *)) {
void (^errorHandler)(NSError *error) = ^(NSError *error) {
NSLog(@"強(qiáng)制%@錯(cuò)誤:%@", isLaunchScreen ? @"橫屏" : @"豎屏", error);
};
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Warc-performSelector-leaks"
SEL supportedInterfaceSelector = NSSelectorFromString(@"setNeedsUpdateOfSupportedInterfaceOrientations");
[self performSelector:supportedInterfaceSelector];
NSArray *array = [[UIApplication sharedApplication].connectedScenes allObjects];
UIWindowScene *scene = (UIWindowScene *)[array firstObject];
Class UIWindowSceneGeometryPreferencesIOS = NSClassFromString(@"UIWindowSceneGeometryPreferencesIOS");
if (UIWindowSceneGeometryPreferencesIOS) {
SEL initWithInterfaceOrientationsSelector = NSSelectorFromString(@"initWithInterfaceOrientations:");
UIInterfaceOrientationMask orientation = isLaunchScreen ? UIInterfaceOrientationMaskLandscapeRight : UIInterfaceOrientationMaskPortrait;
id geometryPreferences = [[UIWindowSceneGeometryPreferencesIOS alloc] performSelector:initWithInterfaceOrientationsSelector withObject:@(orientation)];
if (geometryPreferences) {
SEL requestGeometryUpdateWithPreferencesSelector = NSSelectorFromString(@"requestGeometryUpdateWithPreferences:errorHandler:");
if ([scene respondsToSelector:requestGeometryUpdateWithPreferencesSelector]) {
[scene performSelector:requestGeometryUpdateWithPreferencesSelector withObject:geometryPreferences withObject:errorHandler];
}
}
}
#pragma clang diagnostic pop
} else {
[self p_swichToNewOrientation:isLaunchScreen ? UIInterfaceOrientationLandscapeRight : UIInterfaceOrientationPortrait];
}
}
接下來,講解一下關(guān)于視頻的橫豎屏切換
場(chǎng)景:豎屏狀態(tài),視頻只是占了屏幕的一小部分,橫屏需要鋪滿整個(gè)屏幕
視頻橫豎屏切換
先將 redView
占屏幕的一小部分,切換橫豎屏效果如下:
監(jiān)聽橫豎屏狀態(tài)
在這里橫屏狀態(tài)需要鋪滿整個(gè)屏幕,在 iOS16
之前監(jiān)聽 UIDeviceOrientationDidChangeNotification
通知,在監(jiān)聽方法中進(jìn)行對(duì) redView
的約束更新,但是在 iOS16
開始,這個(gè)通知并不會(huì)一定有(我這邊有時(shí)候有,有時(shí)候沒有通知過來),在找了一遍官方文檔之后,發(fā)現(xiàn) UIViewController
從 iOS8
開始就有一個(gè)方法 viewWillTransitionToSize:withTransitionCoordinator:
/*
This method is called when the view controller's view's size is changed by its parent (i.e. for the root view controller when its window rotates or is resized).
當(dāng) UIViewController 的 view 大小被其父級(jí)更改時(shí)(即當(dāng)根控制器的窗口旋轉(zhuǎn)或調(diào)整大小時(shí)),將調(diào)用此方法
If you override this method, you should either call super to propagate the change to children or manually forward the change to children.
*/
- (void)viewWillTransitionToSize:(CGSize)size withTransitionCoordinator:(id <UIViewControllerTransitionCoordinator>)coordinator API_AVAILABLE(ios(8.0));
于是在這里就不需要監(jiān)聽 UIDeviceOrientationDidChangeNotification
通知了
- (void)viewWillTransitionToSize:(CGSize)size withTransitionCoordinator:(id<UIViewControllerTransitionCoordinator>)coordinator {
[super viewWillTransitionToSize:size withTransitionCoordinator:coordinator];
NSLog(@"view 發(fā)生改變:%@", NSStringFromCGSize(size));
BOOL isLaunchScreen = NO;
if (@available(iOS 16.0, *)) {
// iOS16 需要使用 UIWindowScene 來區(qū)分橫豎屏
NSArray *array = [[[UIApplication sharedApplication] connectedScenes] allObjects];
UIWindowScene *scene = [array firstObject];
isLaunchScreen = scene.interfaceOrientation == UIInterfaceOrientationLandscapeRight;
} else {
// 這里是 UIDeviceOrientationLandscapeLeft(我們需要 Home 按鍵在右邊)
// UIDeviceOrientationLandscapeLeft, // Device oriented horizontally, home button on the right
isLaunchScreen = [UIDevice currentDevice].orientation == UIDeviceOrientationLandscapeLeft;
}
NSLog(@"將要%@", isLaunchScreen ? @"橫屏" : @"豎屏");
}
打印信息:
<主>-[YXCLaunchScreenController viewWillTransitionToSize:withTransitionCoordinator:] 47行 : view 發(fā)生改變:{812, 375}
<主>-[YXCLaunchScreenController viewWillTransitionToSize:withTransitionCoordinator:] 57行 : 將要橫屏
<主>-[YXCLaunchScreenController viewWillTransitionToSize:withTransitionCoordinator:] 47行 : view 發(fā)生改變:{375, 812}
<主>-[YXCLaunchScreenController viewWillTransitionToSize:withTransitionCoordinator:] 57行 : 將要豎屏
適配 redView
根據(jù)當(dāng)前屏幕方向?qū)?redView 進(jìn)行適配,豎屏占屏幕一部分空間,橫屏占滿整個(gè)屏幕
- (void)viewWillTransitionToSize:(CGSize)size withTransitionCoordinator:(id<UIViewControllerTransitionCoordinator>)coordinator {
[super viewWillTransitionToSize:size withTransitionCoordinator:coordinator];
NSLog(@"view 發(fā)生改變:%@", NSStringFromCGSize(size));
BOOL isLaunchScreen = NO;
if (@available(iOS 16.0, *)) {
NSArray *array = [[[UIApplication sharedApplication] connectedScenes] allObjects];
UIWindowScene *scene = [array firstObject];
isLaunchScreen = scene.interfaceOrientation == UIInterfaceOrientationLandscapeRight;
} else {
// 這里是 UIDeviceOrientationLandscapeLeft(我們需要 Home 按鍵在右邊)
// UIDeviceOrientationLandscapeLeft, // Device oriented horizontally, home button on the right
isLaunchScreen = [UIDevice currentDevice].orientation == UIDeviceOrientationLandscapeLeft;
}
NSLog(@"將要%@", isLaunchScreen ? @"橫屏" : @"豎屏");
[self p_updateViewWithIsLaunchScreen:isLaunchScreen size:size];
}
/// 適配橫豎屏約束
/// - Parameters:
/// - isLaunchScreen: 是否是橫屏
/// - size: 當(dāng)前控制器 View 的 size 大小
- (void)p_updateViewWithIsLaunchScreen:(BOOL)isLaunchScreen size:(CGSize)size {
if (isLaunchScreen) {
// 橫屏
[self.redView mas_remakeConstraints:^(MASConstraintMaker *make) {
make.edges.equalTo(self.view);
}];
} else {
// 豎屏
[self.redView mas_remakeConstraints:^(MASConstraintMaker *make) {
make.center.left.right.equalTo(self.view);
make.height.mas_equalTo(150);
}];
}
}
最后的效果如圖:
以上就是關(guān)于 iOS16 橫豎屏適配以及關(guān)于視頻適配的全部?jī)?nèi)容