The project I am working on currently has multiple storyboards. we are programatically moving from the first storyboard to the next using the following code:
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let vc = storyboard.instantiateViewController(withIdentifier: "DiscoverViewController") as UIViewController
self.present(vc, animated: true, completion: nil)
the storyboard it is moving to has the Storyboard Entry Point pointed at the Navigation Controller of the view controller that is being instantiated and presented above.
The problem is that when the the page is displayed the navigation bar doesn't appear. It appears on the storyboard view, and before we separated the storyboards we weren't having this issue.
Is there an issue with the way I am going from one storyboard to the other? Do I need to present the navigation controller rather than the view controller?
thanks!
This is because you're presenting the view controller, when instead you probably want to push it onto the navigation stack.
This answer shows what you need to do. Here is the code, adapted for your example:
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let vc = storyboard.instantiateViewController(withIdentifier: "DiscoverViewController") as UIViewController
self.navigationController?.pushViewController(vc, animated: true)
Related
Want to navigate between two UIStoryBoard and between different UIViewController. My problem is that when I tap on UIButton the control should transfer to the UIViewController from current UIStoryBoard to another UIStoryBoard.
Any help is appreciated.
You can either Push or Present the new viewController
Using Following code you can push the new view controller to your navigation controller
let yourVC = UIStoryboard(name: StoryBoardName, bundle: nil).instantiateViewController(withIdentifier: ViewConrtollerID) as! YourViewController
self.navigationController?.pushViewController(yourVC, animated: true)
You first need to create reference of the second storyboard in which you want to move from current. For that you need to create reference as shown in below image and set the other story board name in that.
after that you can push the to that storyboard controller using below code.
let vc = UIStoryboard(name:"storyboadnameToPush", bundle: nil).instantiateViewController(withIdentifier:controller storyboardId)
self.navigationController?.pushViewController(vc, animated: true)
In my app, I have a UINavigationController that i'm pushing and popping ViewControllers from. At some point, I want to show a VC modally (showing the previous controller "underneath"). I can get it to work by setting up a segue in a storyboard, however I'm in a spot where I need to do it programmatically, and I can't seem to find the right magic incantation to make it work.
I saw a couple of similar questions but they seemed to be showing the UINavigationController modally, not showing one of the VC's on the UINavigationController stack modally.
(I put up a test application here: https://github.com/SuperTango/ModalNavController, and that's where this code and images come from)
The "Manual" code does:
#IBAction func goToVC2Tapped(_ sender: Any) {
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let destinationViewController = storyboard.instantiateViewController(withIdentifier: "VC2ViewController") as! VC2ViewController
destinationViewController.modalPresentationStyle = UIModalPresentationStyle.overCurrentContext
self.navigationController?.modalPresentationStyle = UIModalPresentationStyle.overCurrentContext
self.navigationController?.pushViewController(destinationViewController, animated: true)
}
but it's not working (see the second transition in the gif below).
The segue that works is setup like this:
This gif is from the test app and shows how it works with the segue, but not manually.
Any ideas? Thanks!
To present modally you need to use:
present(destinationViewController, animated: true, completion: { })
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let destinationViewController = storyboard.instantiateViewController(withIdentifier: "YourVC") as! YourVC
destinationViewController.modalPresentationStyle = UIModalPresentationStyle.overCurrentContext
destinationViewController.modalTransitionStyle = .crossDissolve
self.present(destinationViewController, animated: false, completion: nil)
I think this helps show your ViewController in a modal way of presenting. Write the above code under outlet actions or where you want to present your ViewController.
Prior to iOS 11, the UINavigationBar buttons and title are being displayed correctly.
After downloaded Xcode 9 with iOS 11 and, after building and running without doing changes, both navigation buttons and the title are not being displayed anymore.
For presenting ViewController I am using below code, its might be an issue of adding viewcontroller in navigation rootviewcontroller:
if let vc = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "MyVC") as? MyVC {
let navigationController = UINavigationController(rootViewController: vc)
self.present(navigationController, animated: true, completion: nil)
}
Any alternate of the above code to present viewcontroller and also support navigationcontroller which are presenting.
Any help is appriciated..!!
On my story board I have a collection view controller embedded in a navigation controller. Now from the cell of the collection view, I programaticly open the second view controller (called TwoPicsViewController), like this:
let storyBoard : UIStoryboard = UIStoryboard(name: "Main", bundle:nil)
let nextViewController = storyBoard.instantiateViewController(withIdentifier: "TwoPicsViewController") as! TwoPicsViewController
self.present(nextViewController, animated:true, completion:nil)
However when my TwoPicsViewController opens, I don't see the navigation bar at all. On the story board I connected the TwoPicsViewController to the first view controller with a segue show.
Am I missing something else?
Thanks
To push a new UIViewController to the navigation stack you should use:
self.navigationController?.pushViewController(nextViewController, animated: true)
Instead of:
self.present(nextViewController, animated:true, completion:nil)
You are not putting the new vc on the navigation stack, try this:
performSegue(withIdentifier: "Segue Name", sender: nil)
And on Storyboard select the Segue, attributes inspector, identifier = "Segue Name"
I've just finished a Swift tutorial which does not use Interface Builder. It is all programmatic. Everything looks great but now I have to segue back to the storyboard, I am lost.
I'd like to segue from the "Login" button designed in the Audible tutorial to the next view controller I created in my storyboard called "DashboardVC".
Here is a link to the tutorial and source code. https://www.letsbuildthatapp.com/course_video?id=382
TIA
As is all from code, you don't have segue's; either you have to push a new controller using pushViewController from NavigationController or instantiate a new ViewController, in this case your 'DashboardVC'.
Like this
let viewController = DashboardVC()
viewController.view.backgroundColor = .blueColor() //example
navigationController?.pushViewController(viewController, animated: true)
Or just presenting the controller using this
let vc = DashboardVC()
present(vc, animated: true, completion: nil)
Using Storyboard, should be like this; the view controller identifier have to be set in the storyboard as well
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let viewController = storyboard.instantiateViewController(withIdentifier :"DashboardVC") as! DashboardVC
self.present(viewController, animated: true)