2016-10-18 113 views
0

僅通過將設備左右旋轉切換到另一個視圖控制器是否可能?在旋轉設備時切換到另一個視圖控制器

我會嘗試一下: // LandscapeTabView

override func viewWillLayoutSubviews() { 

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

    } 
    else { 

    } 

但是不知道怎麼在功能填補? 感謝您幫助菜鳥!

回答

0

第一條:您可以訂閱系統通知有關設備旋轉這樣

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

然後使功能

func orientationChanged() {} 

對於正確的狀態判斷,我建議使用這種方法

UIApplication.shared.statusBarOrientation == .portrait 

如果它的真實肖像,虛假景觀

因此,依賴於狀態,例如,您可以一些vc在景觀和pop它當設備被轉回。 爲推動您可以輕鬆地創建視圖控制器的實例一樣,

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」)。

在這裏你去:)

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協議程序(iOS 8 +) 。

然後你可以添加一個子視圖控制器到你的控制器並控制它應該如何動畫。您可以將其用作參考: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... 
} 
相關問題