现象:
4.0.5版本上,乐视的转屏没有问题,但4.2版本上,乐视转屏会导致整个屏幕布局混乱
原因:
我们代码的底层为
UITabBarController写了一个超类
UITabBarController+HDFUITabBarController.m
- (BOOL)shouldAutorotate {
return NO;
}
-(UIInterfaceOrientationMask)supportedInterfaceOrientations {
return UIInterfaceOrientationMaskPortrait;
}
-(UIInterfaceOrientation)preferredInterfaceOrientationForPresentation {
return UIInterfaceOrientationPortrait;
return NO;
}
-(UIInterfaceOrientationMask)supportedInterfaceOrientations {
return UIInterfaceOrientationMaskPortrait;
}
-(UIInterfaceOrientation)preferredInterfaceOrientationForPresentation {
return UIInterfaceOrientationPortrait;
}
这几句代码的效果是把整个app中所有的页面的转屏开关给关闭了,而乐视4.2demo正确转屏的执行前提是需要打开这些按钮的,所以,直接替换乐视4.2的转屏demo会出现导航栏布局全部混乱的问题
解决办法:
1、找到appDelegate中方法:
-(
UIInterfaceOrientationMask
)application:(
UIApplication
*)application supportedInterfaceOrientationsForWindow:(
UIWindow
*)window
这个类会在每次手机转屏前触发,这个类返回的是一个mask码
返回哪个方向就表示app当前支持哪个方向的转屏,
2、设置一个常驻内存单例类:
HDFInterfaceOrientationsManager,控制那些页面可以转屏,哪些页面不可以转屏,
init方法
默认不可转屏,
+ (instancetype) sharedManager {
static dispatch_once_t once;
static id instance;
dispatch_once(&once, ^{
instance = [self new];
});
return instance;
}
- (instancetype)init {
if ((self = [super init])) {
_supportAllOrientations = NO;
}
return self;
}
static dispatch_once_t once;
static id instance;
dispatch_once(&once, ^{
instance = [self new];
});
return instance;
}
- (instancetype)init {
if ((self = [super init])) {
_supportAllOrientations = NO;
}
return self;
}
3、appDelegate中设置如下,如果当前页面转屏开关没有打开,则默认返回
UIInterfaceOrientationMaskPortrait,不支持转屏;如果当前页面转屏开关有打开,则检测当前:
-(
UIInterfaceOrientationMask
)application:(
UIApplication
*)application supportedInterfaceOrientationsForWindow:(
UIWindow
*)window
{
{
if
([[
HDFInterfaceOrientationsManager
sharedManager
]
supportAllOrientations
]) {
//这里是控制转屏状态下的弹窗展示方向,要和当前屏幕方向一致
if
(window.
windowLevel
==
UIWindowLevelAlert
) {
if
([application
statusBarOrientation
]==
UIInterfaceOrientationPortrait
) {
return
UIInterfaceOrientationMaskPortrait
;
}
if ([application statusBarOrientation ]== UIInterfaceOrientationLandscapeLeft ) {
return UIInterfaceOrientationMaskLandscapeLeft ;
}
if ([application statusBarOrientation ]== UIInterfaceOrientationLandscapeRight ) {
return UIInterfaceOrientationMaskLandscapeRight ;
}
}
return UIInterfaceOrientationMaskAllButUpsideDown ;
}
else {
return UIInterfaceOrientationMaskPortrait ;
}
}
if ([application statusBarOrientation ]== UIInterfaceOrientationLandscapeLeft ) {
return UIInterfaceOrientationMaskLandscapeLeft ;
}
if ([application statusBarOrientation ]== UIInterfaceOrientationLandscapeRight ) {
return UIInterfaceOrientationMaskLandscapeRight ;
}
}
return UIInterfaceOrientationMaskAllButUpsideDown ;
}
else {
return UIInterfaceOrientationMaskPortrait ;
}
}
4、在需要支持转屏的页面
- 页面即将展示时 [HDFInterfaceOrientationsManager sharedManager].supportAllOrientations = YES;
- 页面即将消失时 [HDFInterfaceOrientationsManager sharedManager].supportAllOrientations = NO;
苹果文档上:
UITabBarController is rotatable if all of its view controllers are rotatable.
UITabBarController在他挂的控制器都可旋转的情况下,也是可旋转的。