项目仅支持竖屏,特定页面支持横屏
项目概述:
1.项目有UITabBarController管理多个UINavigationController,每个UINaviagitionController分管多个UIViewController;
2.项目由多人开发,为不相互干扰使用多个UIStoryboard;
项目需求:
1.工程中只有播放视频页面允许用户横屏播放,但并非打开页面就是横屏;
2.播放页面需要展示title,选择用push方式跳转,不能用模态方式弹出页面;
3.播放页面横屏展示时,导航栏自动隐藏,单击屏幕可恢复导航栏,再次单击可再次隐藏;
4.播放完成后返回上级页面,只能是竖屏展示;
解决方案:
1.先将项目选中三方向都支持;
1.选中工程;
2.选中TAGETS;
3.选中General;
4.勾选Landscape Left和Landscape Right;
(如下图:)
2.构建UITabBarController控制器子类,并在控制器中写如下代码:
// 是否允许转向
- (BOOL)shouldAutorotate
{
return self.viewControllers.firstObject.shouldAutorotate;
}
{
return self.viewControllers.firstObject.shouldAutorotate;
}
// 允许转哪些方向
- (UIInterfaceOrientationMask)supportedInterfaceOrientations
{
return [self.viewControllers.firstObject supportedInterfaceOrientations];
}
- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation
{
return [self.viewControllers.firstObject preferredInterfaceOrientationForPresentation];
{
return [self.viewControllers.firstObject supportedInterfaceOrientations];
}
- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation
{
return [self.viewControllers.firstObject preferredInterfaceOrientationForPresentation];
}
3.构建UINaviagitionController控制器子类,并在控制器中写如下代码:
- (BOOL)shouldAutorotate
{
return self.visibleViewController.shouldAutorotate;
}
- (UIInterfaceOrientationMask)supportedInterfaceOrientations
{
return [self.viewControllers.lastObject supportedInterfaceOrientations];
}
- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation
{
return [self.viewControllers.lastObject preferredInterfaceOrientationForPresentation];
{
return self.visibleViewController.shouldAutorotate;
}
- (UIInterfaceOrientationMask)supportedInterfaceOrientations
{
return [self.viewControllers.lastObject supportedInterfaceOrientations];
}
- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation
{
return [self.viewControllers.lastObject preferredInterfaceOrientationForPresentation];
}
4.在允许转向的UIViewController页面写如下代码:
- (BOOL)shouldAutorotate
{
return YES;
}
- (UIInterfaceOrientationMask)supportedInterfaceOrientations
{
return UIInterfaceOrientationMaskAll;
{
return YES;
}
- (UIInterfaceOrientationMask)supportedInterfaceOrientations
{
return UIInterfaceOrientationMaskAll;
}
5.在不允许转向的UIViewController页面写如下代码:
- (BOOL)shouldAutorotate
{
return NO;
}
- (UIInterfaceOrientationMask)supportedInterfaceOrientations
{
return UIInterfaceOrientationMaskPortrait;
{
return NO;
}
- (UIInterfaceOrientationMask)supportedInterfaceOrientations
{
return UIInterfaceOrientationMaskPortrait;
}
注意事项:
1.这里最主要犯错点,要认真填写UITabBarController中代码内人,他与UINaviagitionController中代码是有却被的,区别如下:
UITabBarController中return的是self.viewControllers.firstObject;
UINaviagitionController中return的是self.viewControllers.lastObject;
而且允许的方向也是有所不同,分别为:
UITabBarController中的self.viewControllers.firstObject.shouldAutorotate;
UINaviagitionController中的self.visibleViewController.shouldAutorotate;
2.由于项目中设置TAGETS是对整个项目的设置,设置了上、左和右方向后,也就等于支持了整个工程,所以不仅仅要在从允许横屏页面返回的上一级页面设置shouldAutorotate为NO,而是要在工程项目中的每一个控制器页面设置,我简单的写成带有 viewDidLoad的页面,就要复制那两个方法。