2016-10-18 6 views
0

デバイスを左右に動かすだけで別のビューコントローラに切り替えることはできますか?デバイスを回転させながら別のビューコントローラに切り替え

私はそれをしようとするだろう: // LandscapeTabView

override func viewWillLayoutSubviews() { 

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

    } 
    else { 

    } 

しかし、その機能に記入するのか分からないのですか? ルーキーを助けてくれてありがとう!

答えて

0

まず:あなたはその後、私はこの方法に

UIApplication.shared.statusBarOrientation == .portrait 

を使用することをお勧め決定正しい状態のために機能

func orientationChanged() {} 

を行うデバイスがこの

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

のように回転についてのシステム通知を購読することができます真の肖像画、偽の風景の場合

したがって、状態に依存することができます。例えば、いくつかのvcを表示し、popをデバイスが戻ったときに表示します。あなたは簡単にその

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

のように、ポップのためにあなたのViewControllerのインスタンスを作成することができ押すため :

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

お知らせ:VCあなたは1(メイン)ストーリーボードで店をでなければなりませんインスタンス化しています。また、アイデンティティ・インスペクタの「ストーリーボードID」フィールドにis(ストリング「here_is_vc_id」が入る場所)を設定する必要があります。ここで

あなたは:)

0

を試しに行く私の小さなeffort-

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 
     } 

    } 
0

提案のアプローチが有効であるが、変更のこの種に対応する推奨方法は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... 
} 
関連する問題