在做视频开发时遇到屏幕旋转问题,其中涉及到StatusBar、 UINavigationController、UITabBarController 、UIViewcontroller。

在设备锁屏下的整体效果图

iOS-旋转.gif
主要涉及以下4点:
1、横竖屏旋转
第1步:
-(UIInterfaceOrientationMask)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window {
// NSLog(@"0000000---------%@",NSStringFromClass([[self topViewController] class]));
// if ([NSStringFromClass([[self topViewController] class]) isEqualToString:@"FirstViewController"]) {
// //横屏
// return UIInterfaceOrientationMaskLandscapeRight;
// }
// //竖屏
// return UIInterfaceOrientationMaskPortrait;
NSUInteger orientations = UIInterfaceOrientationMaskAllButUpsideDown;
if(self.window.rootViewController){
//取出当前显示的控制器
UIViewController *presentedViewController = [self topViewControllerWithRootViewController:self.window.rootViewController];
//按当前控制器支持的方向确定旋转方向(将旋转方向重新交给每个控制器自己控制)
NSLog(@"%s, line = %d",__FUNCTION__,__LINE__);
orientations = [presentedViewController supportedInterfaceOrientations];
}
return orientations;
}
//获取界面最上层的控制器
//- (UIViewController*)topViewController {
// NSLog(@"%s, line = %d",__FUNCTION__,__LINE__);
// return [self topViewControllerWithRootViewController:[UIApplication sharedApplication].keyWindow.rootViewController];
//}
//一层一层的进行查找判断
- (UIViewController*)topViewControllerWithRootViewController:(UIViewController*)rootViewController {
NSLog(@"%s, line = %d",__FUNCTION__,__LINE__);
if ([rootViewController isKindOfClass:[UITabBarController class]]) {
UITabBarController* tabBarController = (UITabBarController*)rootViewController;
NSLog(@"Tabbar:%@",NSStringFromClass([tabBarController.selectedViewController class]));
return [self topViewControllerWithRootViewController:tabBarController.selectedViewController];
} else if ([rootViewController isKindOfClass:[UINavigationController class]]) {
UINavigationController* nav = (UINavigationController*)rootViewController;
NSLog(@"nav:%@",NSStringFromClass([nav.visibleViewController class]));
return [self topViewControllerWithRootViewController:nav.visibleViewController];
} else if (rootViewController.presentedViewController) {
NSLog(@"present:%@",NSStringFromClass([rootViewController.presentationController class]));
UIViewController* presentedViewController = rootViewController.presentedViewController;
return [self topViewControllerWithRootViewController:presentedViewController];
} else {
NSLog(@"root:%@",rootViewController);
return rootViewController;
}
}另外有需要云服务器可以了解下创新互联建站www.cdcxhl.com,海内外云服务器15元起步,三天无理由+7*72小时售后在线,公司持有idc许可证,提供“云服务器、裸金属服务器、高防服务器、香港服务器、美国服务器、虚拟主机、免备案服务器”等云主机租用服务以及企业上云的综合解决方案,具有“安全稳定、简单易用、服务可用性高、性价比高”等特点与优势,专为企业上云打造定制,能够满足用户丰富、多元化的应用场景需求。