2017-08-05 97 views
1

我正在iOS版Swift中開發應用程序。我的應用程序的UI使用收件箱UISplitViewController。當我的應用以縱向方向在iPad上運行時,我希望左側視圖控制器彈出窗口在選擇某個項目後自動隱藏。如何以編程方式在iOS 10中的分屏視圖控制器中隱藏左視圖控制器

唯一的答案我已經能夠找到使用方法不再存在或被棄用。

更具體地說,一旦表格中的項目被選中,我想隱藏下面屏幕截圖左側的視圖。

Screenshot of app with menu visible.

回答

1

首先,確保你有一個從應用程序的委託安裝到你的UISplitViewController參考。例如。

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool { 
    let splitViewController = window!.rootViewController as! UISplitViewController 
    splitViewController.preferredDisplayMode = UISplitViewControllerDisplayMode.primaryOverlay 
    let navigationController = splitViewController.viewControllers[splitViewController.viewControllers.count-1] as! UINavigationController 
    splitViewController.delegate = self 
} 

然後在你爲你的表視圖didSelectRowAtIndexPath方法,添加類似:

if UIApplication.shared.statusBarOrientation == .portrait { 
     splitViewController?.preferredDisplayMode = .primaryHidden 
    } 

要動態:

if UIApplication.shared.statusBarOrientation == .portrait { 
     UIView.animate(withDuration: 0.3, animations: { 
      self.splitViewController?.preferredDisplayMode = .primaryHidden 
     }, completion: nil) 
    } 
+0

我接受了這個作爲解決方案,因爲它做什麼我問。但是,如果「主要」視圖可以滑出視圖而不是消失,那將會很好。 –

+0

好點 - 用動畫代碼更新了答案。 – Tom

相關問題