iOS 屏幕旋转

第一个方法:

参考: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];






  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值