只在特定的页面支持屏幕旋转

只在特定的页面支持屏幕旋转

  • 最近做项目还是遇到了一些问题,比如说只在一个播放视频的页面支持旋转,其他页面不支持。看了网上好多的方法,大部分都没用,有的还写的那么复杂,结果屁用没有。
  • 这个是通用的,大家都这么写(我自定义的tabbar和navgation结果没有用,继续往下看,最后一个可以)
  • 首先在UINavigationController里面设置那些界面可以旋转
  • //允许旋转
    • (BOOL)shouldAutorotate
      {
      return YES;
      }
      /**
  • topviewController就是当前导航控制器所控制的viewcontrol
    */
    • (UIInterfaceOrientationMask)supportedInterfaceOrientations
      {
      if ([self.topViewController isKindOfClass:[ViewController class]])
      {
      return self.topViewController.supportedInterfaceOrientations;
      }
      return UIInterfaceOrientationMaskPortrait;
      }

//在需要旋转的界面中写方法
- (void)viewWillAppear:(BOOL)animated{
[self interfaceOrientation:UIInterfaceOrientationLandscapeLeft];
}

  • (void)viewDidDisappear:(BOOL)animated{
    [self interfaceOrientation:UIInterfaceOrientationPortrait];
    }

  • (void)interfaceOrientation:(UIInterfaceOrientation)orientation
    {
    //强制转换
    if ([[UIDevice currentDevice] respondsToSelector:@selector(setOrientation:)]) {
    SEL selector = NSSelectorFromString(@”setOrientation:”);
    NSInvocation * invocation = [NSInvocation invocationWithMethodSignature:[UIDevice instanceMethodSignatureForSelector:selector]];
    [invocation setSelector:selector];
    [invocation setTarget:[UIDevice currentDevice]];
    int val = orientation;
    [invocation setArgument:&val atIndex:2];
    [invocation invoke];
    }
    }

  • 或者强制横屏 (这个谁都会,但是不适用,往下看)

    • (void)loadView{

    self.view = [[UIView alloc]init];

    CGRect frame = [UIScreen mainScreen].bounds;

    AppDelegate app = (AppDelegate )[UIApplication sharedApplication].delegate;

    UIDeviceOrientation duration = [[UIDevice currentDevice]orientation];

    if (duration == UIDeviceOrientationLandscapeLeft) {

    app.window.transform = CGAffineTransformMakeRotation(M_PI*0.5);
    
    [[UIApplication sharedApplication] setStatusBarOrientation:UIDeviceOrientationLandscapeLeft animated:YES];
    

    }else{

    app.window.transform = CGAffineTransformMakeRotation(M_PI*1.5);
    
    [[UIApplication sharedApplication] setStatusBarOrientation:UIDeviceOrientationLandscapeRight animated:YES];
    

    }

    app.window.bounds = CGRectMake(0, 0, frame.size.height, frame.size.width);

}

  • 其实就是这么简单 (自定义tabbar 和nav 完全好用,当时心里一万头草泥马路过)
    • (UIInterfaceOrientationMask)application:(UIApplication )application supportedInterfaceOrientationsForWindow:(UIWindow )window
      {
      if (_allowRotation == 1) {
      return UIInterfaceOrientationMaskAll;
      }else{
      return (UIInterfaceOrientationMaskPortrait);
      }
      }

// 支持设备自动旋转
- (BOOL)shouldAutorotate
{
if (_allowRotation == 1) {
return YES;
}
return NO;
}

然后在你需要横屏的页面 allowRotation=1;就可以了

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值