当App启动的时候需要强制竖屏,进入App后允许横屏或者竖屏的解决办法:
1、修改App-Info.plist文件只支持UIInterfaceOrientationPortrait方向,如下:
<key>UISupportedInterfaceOrientations</key>
<array>
<string>UIInterfaceOrientationPortrait</string>
</array>
2、在AppDelegate.m文件中添加如下代码:
- (UIInterfaceOrientationMask)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window {
return UIInterfaceOrientationMaskAllButUpsideDown;
}
3、新建一个导航控制器的分类,添加如下代码:
#import "UINavigationController+Orientation.h"
@implementation UINavigationController (Orientation)
/**
* 导航控制器旋转问题
* 只想在某一个类可以进行旋转,其他类不能旋转.
* 扩展导航控制器方法,写以下几个方法
*/
/// 是否自动旋转
- (BOOL)shouldAutorotate
{
return [[self.viewControllers lastObject] shouldAutorotate];
}
/// 支持的方向
- (NSUInteger)supportedInterfaceOrientations
{
return [[self.viewControllers lastObject] supportedInterfaceOrientations];
}
/// 返回优先的方向 (此方法不需要写,如果写了,App在横屏时候回到后台,再重新打开App,界面会竖屏显示)
- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation
{
return [[self.viewControllers lastObject] preferredInterfaceOrientationForPresentation];
}
@end
4、新建一个UITabBarController的分类,添加如下代码:
- (UIInterfaceOrientationMask)supportedInterfaceOrientations
{
return [self.selectedViewController supportedInterfaceOrientations];
}
/// 返回优先的方向 (此方法不需要写,如果写了,App在横屏时候回到后台,再重新打开App,界面会竖屏显示)
- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation
{
return [self.selectedViewController preferredInterfaceOrientationForPresentation];
}
- (BOOL)shouldAutorotate {
return self.selectedViewController.shouldAutorotate;
}
5、新建一个基类控制器,里面添加如下代码:
@property (nonatomic, assign, getter=isAutoRotate) BOOL autoRotate;
/// 是否自动旋转
- (BOOL)shouldAutorotate {
return self.isAutoRotate;
}
/// 支持的方向
- (UIInterfaceOrientationMask)supportedInterfaceOrientations {
return self.isAutoRotate ? UIInterfaceOrientationMaskAllButUpsideDown : UIInterfaceOrientationMaskPortrait;
}
/// 返回优先的方向(此方法不需要写,如果写了,App在横屏时候回到后台,再重新打开App,界面会竖屏显示)
- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation
{
return UIInterfaceOrientationPortrait;
}
当某一个控制器需要进行旋转的时候,直接写self.isAutoRotate = YES
就可以啦~
网友评论