第一个方法:
参考:https://www.aliyun.com/jiaocheng/371722.html
步骤1:
然后创建自定义的UITabBarController 和 UINavigationController
在自定义的UITabBarController.m中添加
-(BOOL)shouldAutorotate{
return [self.selectedViewController shouldAutorotate];
}
//所支持的方法集合,是一个枚举类
- (UIInterfaceOrientationMask)supportedInterfaceOrientations {
return [self.selectedViewController supportedInterfaceOrientations];
}
在自定义的UINavigationController.m中添加
-(BOOL)shouldAutorotate{
return [self.visibleViewController shouldAutorotate];
}
//所支持的方法集合,是一个枚举类
- (UIInterfaceOrientationMask)supportedInterfaceOrientations {
return [self.visibleViewController supportedInterfaceOrientations];
}
步骤2:
在AppDelegate.m中使用
-(UIInterfaceOrientationMask)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window{
return UIInterfaceOrientationMaskAllButUpsideDown;
}
-(BOOL)shouldAutorotate{
return YES;
}
//所支持的方法集合,是一个枚举类
- (UIInterfaceOrientationMask)supportedInterfaceOrientations {
return UIInterfaceOrientationMaskPortrait;
}
在创建的rootViewController时使用自定义的UINavigationController
RootViewController *rootVC = [[RootViewController alloc] init];
NAViewController *rootNa = [[NAViewController alloc] initWithRootViewController:rootVC];
self.window.rootViewController = rootNa;
步骤3:
在使用的ViewController中如果是横屏的
//#pragma mark - 控制屏幕旋转方法
//是否自动旋转,返回YES可以自动旋转,返回NO禁止旋转
- (BOOL)shouldAutorotate{
return YES;
}
//返回支持的方向
- (UIInterfaceOrientationMask)supportedInterfaceOrientations{
return UIInterfaceOrientationMaskLandscapeRight;
}
//由模态推出的视图控制器 优先支持的屏幕方向
- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation{
return UIInterfaceOrientationLandscapeRight;
}
如果是竖屏的
//#pragma mark - 控制屏幕旋转方法
//是否自动旋转,返回YES可以自动旋转,返回NO禁止旋转
- (BOOL)shouldAutorotate{
return NO;
}
//返回支持的方向
- (UIInterfaceOrientationMask)supportedInterfaceOrientations{
return UIInterfaceOrientationMaskPortrait;
}
//由模态推出的视图控制器 优先支持的屏幕方向
- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation{
return UIInterfaceOrientationPortrait;
}
当使用的是push方式跳转时不用添加额外的方式
当使用present方式模态跳转时,使用自定义的UINavigationController进行跳转:
NAViewController *rootNa = [[NAViewController alloc] initWithRootViewController:rootVC];
[self presentViewController: rootNa animated:YES completion:nil];
额外备注:
模态跳转有导航栏的在使用:
- (void)viewWillAppear:(BOOL)animated {
//设置导航栏背景图片为一个空的image,这样就透明了
[self.navigationController.navigationBar setBackgroundImage:[[UIImage alloc] init] forBarMetrics:UIBarMetricsDefault];
[self.navigationController.navigationBar setShadowImage:[[UIImage alloc] init]];//去掉透明后导航栏下边的黑边
}
跳转回来发生屏幕方向自动变了
第二种方法:
1.AppDelegate的下面这个方法(默认不存在,需要手动添加)随着页面的每次跳转都会回调一次,所以可以在这个方法设置方向
-(UIInterfaceOrientationMask)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window{
}
在AppDelegate中设置一个方向属性,然后根据属性判断返回相应的方向值
AppDelegate.h文件
@property(nonatomic,assign) NSInteger allowRotation;
AppDelegate.m文件
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
/**
*旋转方向
UIInterfaceOrientationMaskPortrait = (1 << UIInterfaceOrientationPortrait),
UIInterfaceOrientationMaskLandscapeLeft = (1 << UIInterfaceOrientationLandscapeLeft),
UIInterfaceOrientationMaskLandscapeRight = (1 << UIInterfaceOrientationLandscapeRight),
UIInterfaceOrientationMaskPortraitUpsideDown = (1 << UIInterfaceOrientationPortraitUpsideDown),
UIInterfaceOrientationMaskLandscape = (UIInterfaceOrientationMaskLandscapeLeft | UIInterfaceOrientationMaskLandscapeRight),
UIInterfaceOrientationMaskAll = (UIInterfaceOrientationMaskPortrait | UIInterfaceOrientationMaskLandscapeLeft | UIInterfaceOrientationMaskLandscapeRight | UIInterfaceOrientationMaskPortraitUpsideDown),
UIInterfaceOrientationMaskAllButUpsideDown = (UIInterfaceOrientationMaskPortrait | UIInterfaceOrientationMaskLandscapeLeft | UIInterfaceOrientationMaskLandscapeRight),
**/
self.allowRotation = UIInterfaceOrientationMaskPortrait;
return YES;
}
-(UIInterfaceOrientationMask)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window{
return self.allowRotation;
}
改变屏幕方向代码
//设置屏幕朝向
+(void)setScreenOrientation:(NSInteger)type{
AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
appDelegate.allowRotation = type;
/*NSNumber *orientationUnknown = [NSNumber numberWithInt:UIInterfaceOrientationUnknown];
[[UIDevice currentDevice] setValue:orientationUnknown forKey:@"orientation"];
NSNumber *orientationTarget = [NSNumber numberWithInt:UIDeviceOrientationPortrait];
if (type == 1) {
//横屏
orientationTarget = [NSNumber numberWithInt:UIInterfaceOrientationLandscapeLeft];
}
[[UIDevice currentDevice] setValue:orientationTarget forKey:@"orientation"];*/
}
例:
[AppConfig setScreenOrientation:UIInterfaceOrientationMaskLandscapeRight];