Missing Title Bar in Detail View of UISplitViewController

被刻印的时光 ゝ 提交于 2020-01-24 10:49:27

问题


I have a UISplitViewController embedded in a container view (because it's not at the root of my app), the mechanics of which are working nicely except for one issue: The navigation bar for the detail view is missing on the iPad.

Initial setup is essentially as follows:

  1. In IB, drag a Split View Controller onto the storyboard, which creates a Split View Controller, a Navigation Controller, a Table View Controller (Master), a basic View Controller (Detail), and the segues connecting them.
  2. Add a regular View Controller with a Container View. Create an Embed Segue from the Container View to the Split View Controller.
  3. Add another segue from the Prototype Cell to the Detail View Controller, supported by the following code in the Master Controller:

override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
    self.performSegue(withIdentifier: "showDetail", sender: nil)
}

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
    if segue.identifier == "showDetail" {
        let destinationViewController = segue.destination as! DetailViewController
        let path = self.tableView.indexPathForSelectedRow! as  NSIndexPath
        destinationViewController.selectedTrainingId = (self.itemList[path.row] as! MyListItem).id
    }
}
  1. Add the data.

Loading the items into master view and selecting the detail are working.


Here is what it looks like in IB (to save space I show the iPhone layout but the relationships should be visible anyhow):

There are few answers in SO dealing with similar problems. Closest matches suggest adding an own navigation controller for the detail view. I did not understand why that would be necessary because the fact that it works as intended on the iPhone I believe shows that the detail view uses the same navigation controller as the master (root) view. But I gave it a try. Result is, as I suspected, that initially a navigation bar is shown. but as soon as an item is selected the bar disappears. Below is the setup.

In many apps (Messaging, Email, Skype, ...) you can see separate top bars for master and detail views. While technically my app does not absolutely need both, it is not really pretty especially with a colored bar. So, the question is: How can I get the Navigation Bar for the Detail View.


回答1:


The detail view needs its own navigation controller to show a navigation bar on the iPad. So, the second image in the question shows the correct setup with the exception that the detail segue needs to point to the navigation controller.

Key is to get a handle on the detail view in order to set the selected item id, which can be done with a small change to the original code:

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
    if segue.identifier == "showDetail" {

        // Destination View Controller
        let destinationNavigationcontroller: UINavigationController! = segue.destination as! UINavigationController
        let destinationViewController: DetailViewController! = destinationNavigationcontroller.topViewController as! DetailViewController

        // Selected Row ID
        let path = self.tableView.indexPathForSelectedRow! as  NSIndexPath
        destinationViewController.selectedId = (self.itemList[path.row] as! MyListItem).id
    }
}


来源:https://stackoverflow.com/questions/45660039/missing-title-bar-in-detail-view-of-uisplitviewcontroller

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!