主人

时间:2016-08-25 17:50:49

标签: ios swift uistoryboard uisplitviewcontroller

我创建了一个新的拆分视图,在界面构建器中我向主视图添加了2个表视图,并更新了像这样的segues:

storyboard

所有工作都按预期工作,除了在iphone plus上,当我以纵向模式导航到第二个表视图控制器,然后我去横向时,分屏显示在master我的第一个表和详细信息中第二个表。

splitscreen

如果我去第一张桌子上的风景图,在主文件中我有第一张桌子,详细信息是它应该的细节,如果我在主文件中导航,导航将在主文件中发生,因为它应该< / p>

如何让它始终在拆分视图的细节中显示详细视图控制器?

1 个答案:

答案 0 :(得分:0)

我明白了,答案是swift 3.0

// This is called every time the splitview is shown, and it decides what to show in the detail
// Since we need the details to always show the detail, we should always return the detail
// but if we return the detail when we already are on detail it will make the splitview show
// the detail in both views; so we try to detect when we are on detail
func splitViewController(_ splitViewController: UISplitViewController, separateSecondaryFrom primaryViewController:UIViewController) -> UIViewController? {
    var is_detail = false

    // because we have to nav controllers linked to the split view, the primary will always be nav
    if let nav = primaryViewController as? UINavigationController {
        // if the top controller of primary is yet another nav controller, this means we navigated to the detail (follow the arrow, and count the nav controllers :D)
        if let nav2 = nav.topViewController as? UINavigationController {
            // now just to be thourough, we check to see if there is a DetailViewController
            if nav2.topViewController is DetailViewController {
                is_detail = true
            }
        }
    }

    // if the primary is actually a detail, we return nil, and let splitview to do it's job
    if is_detail {
        // splitview will show in the master the previous view controller & in detail the current view controller
        return nil
    } else {
        // splitview will keep the current view controller in master, and add this controller in detail
        let storyboard = UIStoryboard(name: "Main", bundle: nil)
        return storyboard.instantiateViewController(withIdentifier: "DetailView")
    }
}