加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

小胖说swift11-------- ios 进入某个VC强转为横屏,出VC后复原

发布时间:2020-12-14 06:36:48 所属栏目:百科 来源:网络整理
导读:今天做项目,某一个VC需要展现VR展览内容,产品要求这个VC可以横屏查看,因为横屏查看的时候,看的范围比较大,但是其余的VC都是竖屏显示的,为了达到某个VC横屏显示其余VC不变的效果,然后查询资料,撸代码。。 查询过资料之后,大概分为四种实现方式,我使

今天做项目,某一个VC需要展现VR展览内容,产品要求这个VC可以横屏查看,因为横屏查看的时候,看的范围比较大,但是其余的VC都是竖屏显示的,为了达到某个VC横屏显示其余VC不变的效果,然后查询资料,撸代码。。

查询过资料之后,大概分为四种实现方式,我使用的是第四种实现方法。

第一种:重写方法:shouldAutorotate 和supportedInterfaceOrientations

  • 写一个子类CusNavigationController 继承 UINavigationController,在CusNavigationController中重写方法:shouldAutorotate 和 supportedInterfaceOrientations
override var shouldAutorotate: Bool {
        return false
    }

    var supportedInterfaceOrientations: UIInterfaceOrientationMask {
        return .portrait
    }
  • 在AppDelegate中设置RootViewController为CusNavigationController容器的VC
  • 然后再A里边重写shouldAutorotate 和 supportedInterfaceOrientations方法

    Bool {
          return true
      }
    
      UIInterfaceOrientationMask {
          return [.landscapeLeft,.landscapeRight]
      }

    这样的话,就可以支持在A页面的时候旋转VC了。

    第二种:强制转换

  • 注意:Apple在3.0以后都不支持这个办法了,这个办法已经成为了私有的了,但是要跳过App Stroe的审核,需要一点巧妙的办法。
    //swift3的时候orientation这个参数可能报错,直接用0,1,2,3代替方向即可
    NSNumber *orientationTarget = [NSNumber numberWithInt:orientation];
    [[UIDevice currentDevice] setValue:orientationTarget forKey:@"orientation"];

    第三种:通过人为的办法改变view.transform的属性。

  • 这个很繁琐,一般不推荐使用,因为里边的布局需要自己一个view一个view的重新布局,如果又想用的,可以参考:
    http://www.cnblogs.com/mrhgw/archive/2012/07/18/2597218.html(http://www.cnblogs.com/mrhgw/archive/2012/07/18/2597218.html)

    第四种:通过appDelgate和第二种方法结合的方式实现

  • 在appdelegate中设置代码如下:
    /// 设置横屏/竖屏显示
      func application(_ application: UIApplication,supportedInterfaceOrientationsFor window: UIWindow?) -> if changeOrientation {
              else {
              return .portrait
          }
      }
  • 然后再需要实现选择的VC中设置代码如下:

    override viewWillAppear(_ animated: Bool) {
          super.viewWillAppear(animated)
          //打开试图的横屏显示
          AppDelegate.shareInstance().changeOrientation = true
      }
    
      viewWillDisappear(super.viewWillDisappear(animated)
          //将试图还原为竖屏
          AppDelegate.shareInstance().changeOrientation = false
          UIDevice.current.setValue(NSNumber(value: 1),forKey: "orientation")
      }
  • 再通过实现系统的方法,改变view里边的布局

    willAnimateRotation(to toInterfaceOrientation:UIInterfaceOrientation,duration: TimeInterval) {
          if (UIInterfaceOrientationIsPortrait(toInterfaceOrientation)) {
              self.standScreen()
          }
          UIInterfaceOrientationIsLandscape(toInterfaceOrientation)) {
              self.acrossScreen()
          }
      }
    
      standScreen() {
          contentView?.frame = CGRect(x: 0,y: NavigationBarHeight,width: SCREEN_WIDTH,height: SCREEN_HEIGHT)
          self.customNavBar?.width = SCREEN_WIDTH
      }
    
      acrossScreen() { 
         contentView?.frame = SCREEN_HEIGHT,0)">SCREEN_WIDTH)
          SCREEN_HEIGHT
      }
  • 我这里只有一个webview(contentView)和一个导航栏(self.customNavBar)改变一下frame就可以了
我的简书链接:http://www.jianshu.com/p/0d5ea1515f7d

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读