Error text:
Application tried to present modally an active controller.
Moreover, all elements seem to be called for the first time. The application crashes most likely due to the navigation controller. How can I fix this error?
#objc private func taskButtonTouched(){
let vc = TaskVC()
let navigationController = OptionalNC(rootViewController: vc)
navigationController.modalPresentationStyle = .fullScreen
self.present(vc, animated: true, completion: nil)
}
You must present OptionalNC insetead TaskVC
#objc private func taskButtonTouched(){
let vc = TaskVC()
let navigationController = OptionalNC(rootViewController: vc)
navigationController.modalPresentationStyle = .fullScreen
self.present(navigationController, animated: true, completion: nil)
}
Related
From my onboarding viewcontroller i need to transition to my tabBarController which is also a navigationController and i want to make it as a root viewcontroller afterwards.
#objc func willGoToMain(sender: UIButton!) {
let tabBarController = TabBarController()
let navigationController = UINavigationController(rootViewController: tabBarController)
navigationController.isNavigationBarHidden = true
self.present(tabBarController, animated: true, completion: nil)
}
Thread 1: "Application tried to present modally a view controller <MyStarterProject.TabBarController: 0x7f9bd8011400> that has a parent view controller <UINavigationController: 0x7f9bd7022800>
I like to transition it like a modal presentation or cross dissolve. not just to appeared as a rootviewcontroller all of a sudden.
Here's what i'm trying to do and this solves my problem.
#objc func willGoToMain(sender: UIButton!) {
guard let window = UIApplication.shared.keyWindow else {
return
}
let tabbarController = TabBarController()
let navigationController = UINavigationController(rootViewController: tabbarController)
navigationController.isNavigationBarHidden = true
window.rootViewController = navigationController
window.makeKeyAndVisible()
let options: UIView.AnimationOptions = .transitionCrossDissolve
let duration: TimeInterval = 0.3
UIView.transition(with: window, duration: duration, options: options, animations: {}, completion:
{ completed in
// maybe do something on completion here
})
}
Change this:
self.present(tabBarController, animated: true, completion: nil)
with this:
self.present(navigationController, animated: true, completion: nil)
Please try this one.
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let mainTabBarController = storyboard.instantiateViewController(identifier: "MainTabBarController")
mainTabBarController.modalPresentationStyle = .fullScreen
self.present(mainTabBarController, animated: true, completion: nil)
If you need more details. please visit this blog
https://fluffy.es/how-to-transition-from-login-screen-to-tab-bar-controller/
I am trying to present a view that is showd from the bottom to the top. This is my code
let myPageViewController = storyboard?.instantiateViewController(withIdentifier: "myPageViewControllerID") as! MyPageViewController
myPageViewController.modalPresentationStyle = .fullScreen
let navController = UINavigationController(rootViewController: myPageViewController)
navigationController?.present(navController, animated: true, completion: nil)
Despite I am using .fullScreen, the view is not presented on full screen.
I tried using peformSegue to show the view with this code
self.performSegue(withIdentifier: "myPageViewSegue", sender: nil)
the page is presented on fullscreen but from the left to the right and not from the bottom to the top.
The third code I tried is this one
let detailVC = MyPageViewController()
let navigationController = UINavigationController(rootViewController: detailVC)
navigationController.modalPresentationStyle = .fullScreen
present(detailVC, animated: true)
here I get an error Application tried to present modally an active controller. I tried to add self.dismiss when disappearing MyPageViewController but it didn't helped.
The problem maybe is you are not presenting navigationController you are presenting detail vc, Use this
'let detailVC = MyPageViewController()
let navigationController = UINavigationController(rootViewController: detailVC)
navigationController.modalPresentationStyle = .fullScreen
present(navigationController, animated: true)'
if problem still exists use
navigationController.modalPresentationStyle = .overCurrentContext
Try this one:
let detailVC = MyPageViewController()
detailVC.modalPresentationStyle = .fullScreen
present(detailVC, animated: true)
You could try this, worked for me:
Setup up your segue:
performSegue(withIdentifier: "myPageViewSegue", sender: self)
Then use the prepare for segue method:
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
let destVC = segue.destination as! MyPageViewSegue
destVC.modalPresentationStyle = .fullScreen
}
I ran into this problem and was stumped. Then I realized that overriding dismiss also overrides .fullscreen
// This breaks .fullscreen
override func dismiss(animated flag: Bool, completion: (() -> Void)? = nil) {
super.dismiss(animated: flag, completion: completion)
// do something
}
It's clear that you don't need this if .fullscreen is being used, but leaving it in place caused problems.
You need to change your code with below lines and then Booom work like a charm
let vc = MyPageViewController()
vc.modalPresentationStyle = .fullScreen
present(vc, animated: true)
let detailVC = MyPageViewController()
let navigationController = UINavigationController(rootViewController: detailVC)
navigationController.modalPresentationStyle = .overCurrentContext
present(detailVC, animated: true, completion: nil)
Explanation:
Its very simple. Its better to explain it line by line. Line 1: Declare the viewcontroller. Line 2: Add Navigation Controller (its optional. If you don't need, then you may skip navigation bar). Line 3: We have defined the modal presentation style link as overCurrentContext. and present the navigationController (it will come up with navigation bar and viewcontroller) or only the viewcontroller as per your need. In this piece of code, you will get the viewcontroller without navigation bar.
I am trying to present NavigationController and select second tab in the completion block. My code is:
let chatViewController = UITabBarController()
let navigationController = UINavigationController(rootViewController: chatViewController)
present(navigationController, animated: true, completion: {
self.visibleViewController = navigationController
self.visibleViewController?.tabBarController?.selectedIndex = 1
})
Second attempt:
let chatViewController = UITabBarController()
let navigationController = UINavigationController(rootViewController: chatViewController)
navigationController.tabBarController?.selectedIndex = 1
present(navigationController, animated: true, completion: {
self.visibleViewController = navigationController
})
In both cases tabBarController is nil. How can I switch to different tab?
You are trying to access UITabBarController of UINavigationController, but you need to access the very first controller from UINavigationController and from there you need to make your UITabBar selected like this way:
func showTabBarControllerr() {
let chatViewController = UITabBarController()
//Note: Make sure you have already added the Controllers in Tabbarcontroller
chatViewController.viewControllers = [DemoOne(), DemoTwo()]
let navigationController = UINavigationController(rootViewController: chatViewController)
present(navigationController, animated: true, completion: {
if let tabBarController = navigationController.viewControllers.first as? UITabBarController {
tabBarController.selectedIndex = 1
}
})
}
Let me know this helps or not!
Instead of calling
present(navigationController, animated: true, completion: { })
in viewDidLoad, try to call it in viewWillAppear or viewDidAppear
Try this (Swift 5):
Set an identifier to your UITabBarController in the Storyboard, something like MainTabBar
Into a function or IBAction, use this code:
let tbc = storyboard?.instantiateViewController(withIdentifier: "MainTabBar") as! UITabBarController
tbc.selectedIndex = 1 // This is the index of your controller
present(tbc, animated: false, completion: nil)
I'm trying to present a view controller and dismiss my current modal view. I saw the answers on StackOverflow, but I don't understand it.
self.dismissViewControllerAnimated(true, completion: {
let vc = self.storyboard?.instantiateViewControllerWithIdentifier("myViewController")
self.presentViewController(vc!, animated: true, completion: nil)
})
Could anyone please tell me, how to do it simply? Thanks
Check your main controller in stack and pop all other view controllers and then present new View Controller.
if let controllers = self.navigationController?.viewControllers {
for vc in controllers {
// Check if the view controller is of MainViewController type
if let myVC = vc as? MainViewController {
self.navigationController?.popToViewController(myVC, animated: false)
}
}
}
Push view controller :
let secondVC = self.storyboard?.instantiateViewController(withIdentifier: "secondVC") as! SecondViewController
self.navigationController?.pushViewController(secondVC, animated: true)
or Present View Controller :
let next = self.storyboard?.instantiateViewController(withIdentifier: "secondVC") as! SecondViewController
self.present(next as UIViewController, animated: true, completion: nil)
You can use this code to Present viewcontroller.
let yourVC = UIStoryboard(name: storyBord, bundle: nil).instantiateViewController(withIdentifier:ID) as! yourViewController
let navController = UINavigationController(rootViewController: yourVC)
self.present(navController, animated: true, completion: {
})
And for dissmissing use this in your presented viewController
self.dismiss(animated: true, completion: {
})
When trying to present a ViewController, on native modules it's crash.
Here is the code on the native side.
#objc(SomethingManager)
class SomethingManager : NSObject {
#objc(doPresent)
func doPresent() {
let vc = UIViewController()
let navigationController = UINavigationController(rootViewController: vc)
navigationController.setNavigationBarHidden(true, animated: false)
let rootViewController = UIApplication.shared.keyWindow?.rootViewController
rootViewController?.dismiss(animated: false, completion: nil)
rootViewController?.present(vc, animated: true, completion: nil)
}
}
the crash said
'Application tried to present modally an active controller <UIViewController: 0x7fb09441e620>`
I've tried to call it on main thread, without any luck
You are trying to present vc, which is already embedded in your navigationController. You should therefore be presenting navigationController and not vc.
rootViewController?.present(navigationController, animated: true, completion: nil)