【问题标题】:switch to another view controller while rotating device旋转设备时切换到另一个视图控制器
【发布时间】:2016-10-18 05:28:56
【问题描述】:

是否只能通过向左/向右转动设备来切换到另一个视图控制器?

我会尝试: //LandscapeTabView

override func viewWillLayoutSubviews() {

    if UIDevice.current.orientation == UIDeviceOrientation.landscapeLeft || UIDevice.current.orientation == UIDeviceOrientation.landscapeRight {  

    } 
    else {

    }

但不知道该填写什么功能? 感谢您帮助一个菜鸟!

【问题讨论】:

    标签: swift view controller switch-statement


    【解决方案1】:

    首先:您可以像这样订阅有关设备旋转的系统通知

    NotificationCenter.default.addObserver(self, selector: #selector(self.orientationChanged), name: NSNotification.Name.UIDeviceOrientationDidChange, object: nil)
    

    然后制作函数

    func orientationChanged() {}
    

    为了正确确定状态,我建议使用此方法

    UIApplication.shared.statusBarOrientation == .portrait
    

    如果是真 - 肖像,假 - 风景

    因此,取决于状态,例如,您可以在横向上push 一些 vc,并在设备返回时 pop 它。 对于推送,您可以像这样轻松创建 ViewController 的实例

    let vc = self.storyboard?.instantiateViewControllerWithIdentifier("here_is_vc_id") as! YourViewController
    

    对于流行音乐:

    _ = navigationController.pushViewController(vc, animated: true)
    

    注意:您要实例化的 VC 必须存储在一个(和主)情节提要中。您还需要在“Storyboard ID”字段的 Identity Inspector 中设置一个 is(字符串“here_is_vc_id”所在的位置)。

    给你:)

    【讨论】:

      【解决方案2】:

      试试我的小努力-

      func rotated()
          {
              if(UIDeviceOrientationIsLandscape(UIDevice.currentDevice().orientation))
              {
                  print("landscapeMode")
                  let nextView = self.storyboard?.instantiateViewControllerWithIdentifier("HomeWorkViewController") as! HomeWorkViewController
              self.navigationController?.pushViewController(nextView, animated: true)
      
              }
      
              if(UIDeviceOrientationIsPortrait(UIDevice.currentDevice().orientation))
              {
                  print("PortraitMode")
                  //As you like
              }
      
          }
      

      【讨论】:

        【解决方案3】:

        建议的方法是有效的,但对此类更改做出反应的推荐方法是使用UIContentContainer 协议 (iOS8+)。

        然后您可以将子视图控制器添加到您的控制器并控制它应该如何动画。您可以以此为参考:Implementing a Container View Controller

        override func viewWillTransitionToSize(size: CGSize, withTransitionCoordinator coordinator: UIViewControllerTransitionCoordinator) {
            super.viewWillTransitionToSize(size, withTransitionCoordinator: coordinator)
        
            let isPortrait = size == UIScreen.mainScreen().fixedCoordinateSpace.bounds.size
        
            // Add a child view controller if landscape, remove it if portrait...
        }
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2021-12-31
          • 2015-03-23
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2013-03-12
          相关资源
          最近更新 更多