简体   繁体   中英

Navigation Bar Across Multiple Storyboards

I've separated my views into multiple storyboards to make managing the project as a team much easier. The only problem is that I want to have the navigation bar across all of my different view controllers. I embedded the first view controller in a navigation controller which added the navigation bar. In my second storyboard, I wanted to constrain a view to the navigation bar, however, since this view controller isn't embedded in a navigation controller, there isn't anything to constrain the view to. I put a navigation bar without a navigation controller to constrain to, however, when I perform the segue from the first storyboard, this results in two navigation bars. How do I make this so that I can constrain the view to the navigation bar without having two of them? Thanks in advance!

I'm not sure your problem. I guess that you want to use ONE navigation bar to constrain many UIViewController in different Storyboard. Here is my answer.

Step 1: Set up Storyboard ID in each UIViewController

故事板 ID

Step 2: push new UIViewController

func switchNextView() {
    let storyboardName:String = "xxxxxx"
    let storyboardId:String = "xxxxx"

    let storyboard = UIStoryboard(name: storyboardName, bundle: nil)
    let vc: UIViewController = 
        storyboard.instantiateViewController(withIdentifier: storyboardId) as! 
        UIViewController
    self.navigationController?.pushViewController(vc, animated: true)
}

You can go from on view controller to another by three way:-

First using segue as you are already doing

second You can Present your view controller by

let storyboard = UIStoryboard(name: "MyStoryboardName", bundle: nil)
let controller = storyboard.instantiateViewController(withIdentifier: "someViewController")
self.present(controller, animated: true, completion: nil) 

Third You can Navigate View controller by

let storyboard = UIStoryboard(name: "MyStoryboardName", bundle: nil)
    let controller = storyboard.instantiateViewController(withIdentifier: "someViewController")
    self.navigationController?.pushViewController(controller, animated: true) 

thanks for helping me out. As mentioned in the comment by @Paulw11 the following worked

"Just constrain to the top layout guide. This will respect the navigation bar at runtime if it is present."

Thanks again for the help! Cheers.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM