Reputation: 4096
I am working in a app which is coded in Objective (only one class) and rest of the app written in Swift. In my app i have 3 storyboards and these storyboards have their viewControllers
accordingly.
Issue: Sometimes i change the
rootViewController
using beow code, the push segues animation stop working after that.
// constant to share globally
let kApplicationDelegate = UIApplication.shared.delegate as? AppDelegate
let controller = UIStoryboard().getControllerInstance(storyBoardName: "Autograph", identifire: "loginSuccessVC")
kApplicationDelegate?.window?.rootViewController = controller
Upvotes: 1
Views: 903
Reputation: 8134
I know it's bit late to reply for this Question! But better late than Never!
First things First:
For Push transition to work properly, you have to have UINavigationController
hosting your current ViewController as the rootViewController
. What the heck does that mean?
Example:
There are two or many ViewControllers (here onwards ViewController == VC) A, B, and more. If you are currently in VC A and you want to move to VC B doing a Push (or Detail) transition on a button action in VC A, then remember your VC A has to be the RootViewController of a UINavigationController
.
[ UI ] [ ] [ ]
[ Navigation ]-- RootViewController ==O>[ A ]--- Push -->[ B ]
[ Controller ] [ ] [ ]
Otherwise it doesn't work. It would appear as a Present Modally transition.
So far there are two ways I have recongised to achieve this. One is pure programmatically, the other is using StoryBoard and Code hand in hand.
Programmatically:
For the ease of explaining I would embed my UINavigationController
at the beginning of the UIViewController
stack. In order to achieve this, we have to do a small alteration to AppDelegate
's didFinishLaunchingWithOptions launchOptions
method. It should look like following:
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
window = UIWindow(frame: UIScreen.main.bounds)
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let initialVCIdentifier = "AViewController" // A ViewController **
let firstVC = storyboard.instantiateViewController(withIdentifier: initialVCIdentifier)
let nav = UINavigationController(rootViewController: firstVC) // Assigning the A VC as the RootViewController of the NavigationController
window?.rootViewController = nav // Look for the *** Note bellow
window?.makeKeyAndVisible()
return true
}
** You have to denote the ViewController Identifier for A VC within the InterfaceBuilder. And I assume that your storyboard name is Main.storyboard.
*** If you are not doing this for initial View controller, just present this UINavigationController as a modal VC within your VC workflow path
In the action for the button in VC A, it should look like following:
@IBAction func TestBtnAct(_ sender: Any) {
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let vc = storyboard.instantiateViewController(withIdentifier: "BViewController") as UIViewController
navigationController?.pushViewController(vc, animated: true)
}
That's it! I think if you have tried it out as I've mentioned above, it should do the magic already.
Storyboard and Code hand-in-hand:
This means your VC A is already being hosted by a UINavigationController
within the storyboard design. In this case you don't need to programmatically create the UINavigationController
and embed the VC A as the RootViewController. Just having the above Button action might be sufficient.
Hope this would be helpful to someone out there. And here is a sample project I have done and uploaded in GitHub for learning purposes of all of yours.
Cheers!
Upvotes: 3
Reputation: 2714
When you are changing the rootViewController
try setting the navigationController
as the root rather than the navigationController
's rootViewController
.
Say you have a viewController let's name it VCA
is the rootViewController of a UINavigationController
Nav1
. Then instead of making VCA
as rootViewController
of kApplicationDelegate?.window?
. Try making Nav1
as the rootViewController
of kApplicationDelegate?.window?
.
kApplicationDelegate?.window?.rootViewController = Nav1
The animation is not working because it may not a push
that's occurring. The self.navigationController
of the ViewController
from where you are pushing might be nil.
Upvotes: 2