设置iOS设备横竖屏,因为iOS16之后有多个window的概念,切换横竖屏需要新的api,因此写一篇文章和演示用的demo供大家参考。
监听横竖屏业务区,涉及控制器,appdelegate等文件
里面核心代码
/// 切换设备方向,赋值appdelegate用于决定屏幕方向。
/// - Parameter isLaunchScreen: 是否是全屏
- (void)p_switchOrientationWithLaunchScreen:(BOOL)isLaunchScreen {
AppDelegate *appdelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
if (isLaunchScreen) {
// 全屏操作
appdelegate.allowRotation = 2;
} else {
// 退出全屏操作
appdelegate.allowRotation = 0;
}
if (@available(iOS 16.0, *)) {
// setNeedsUpdateOfSupportedInterfaceOrientations 方法是 UIViewController 的方法,所以这个操作最好是放在控制器中去操作
[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(@"强制%@错误:%@", isLaunchScreen ? @"横屏" : @"竖屏", error);
}];
} else {
[self p_swichToNewOrientation:isLaunchScreen ? UIInterfaceOrientationLandscapeRight : UIInterfaceOrientationPortrait];
}
}
-(void)setAllowRotation:(NSInteger)allowRotation{
if(_allowRotation !=allowRotation){
_allowRotation = allowRotation;
}
}
- (UIInterfaceOrientationMask)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window {
if (self.allowRotation == 1) {
return UIInterfaceOrientationMaskAllButUpsideDown;
}else if (self.allowRotation == 0){
return UIInterfaceOrientationMaskPortrait;
}else{
UIDeviceOrientation orientation = [UIDevice currentDevice].orientation;
if (orientation == UIDeviceOrientationLandscapeLeft || orientation == UIDeviceOrientationLandscapeRight) {
return UIInterfaceOrientationMaskLandscape;
}else {
return UIInterfaceOrientationMaskLandscape;
}
}
}
/// iOS16 之前进行横竖屏切换方式
需要切换的方向
- (void)p_swichToNewOrientation:(UIInterfaceOrientation)interfaceOrientation {
NSNumber *orientationTarget = [NSNumber numberWithInteger:interfaceOrientation];
[[UIDevice currentDevice] setValue:orientationTarget forKey:@"orientation"];
}
//监听横竖屏变化
- (void)viewWillTransitionToSize:(CGSize)size withTransitionCoordinator:(id<UIViewControllerTransitionCoordinator>)coordinator {
[super viewWillTransitionToSize:size withTransitionCoordinator:coordinator];
BOOL isLaunchScreen = NO;
if (@available(iOS 16.0, *)) {
NSArray *array = [[[UIApplication sharedApplication] connectedScenes] allObjects];
UIWindowScene *scene = [array firstObject];
isLaunchScreen = scene.interfaceOrientation == UIInterfaceOrientationLandscapeRight;
} else {)
// UIDeviceOrientationLandscapeLeft // Device oriented horizontally, home button on the right
isLaunchScreen = [UIDevice currentDevice].orientation == UIDeviceOrientationLandscapeLeft;
}
NSLog(@"将要%@", isLaunchScreen ? @"横屏" : @"竖屏");
[self p_updateViewWithIsLaunchScreen:isLaunchScreen size:size];
}
- (void)p_updateViewWithIsLaunchScreen:(BOOL)isLaunchScreen size:(CGSize)size {
if (isLaunchScreen) {
} else {
}
}