Present viewcontroller with tabbar - swift

I created UITabBarController programmatically in AppDelegate with 4 view controllers(using .xib). When user tap some button on ViewController (VC-A) it present another VC (VC-B) and covered tabbar. So I want to VC-B has a tabbar on the button.
I tried to add VC-B as a child of tabbarcontroller. I tried to .present(vc) and .show(vc) on both: VC-A and VC-A.TabBarController
Creating controllers in AppDelegate:
let controllers = [tabViewController1,tabViewController2,tabViewController3,tabViewController4]
tabBarController.viewControllers = controllers
window?.rootViewController = tabBarController
presenting in VC-A
self.tabBarController?.present(controller, animated: false, completion: nil)

right click and drag from tabbar controller in storyboard to VC-B. that should create a tab on the bottom of your VC-A and VC-B to go back and forth without having to implement any backend code unless you want to animate

The solution is to embed every VC in navigationController and then add to TabBarController.
let vc1 = ViewController1()
let navController1 = UINavigationController(rootViewController: vc1)
navController.isNavigationBarHidden = true
let controllers = [navController1, navController2, navController3, navController4]
tabBarController.viewControllers = controllers
window?.rootViewController = tabBarController
Then call
self.navigationController?.pushViewController(controller, animated:
true)
To diplay VC with tabbar

I will press the red login button at the bottom of the picture and try to log in.
login button => "로그인"
After that, log in.
let moreVC = self.storyboard?.instantiateViewController(withIdentifier: "MoreViewController") as! MoreViewController
moreVC.definesPresentationContext = true
moreVC.modalPresentationStyle = .fullScreen
let navController = UINavigationController(rootViewController: moreVC)
self.present(navController, animated: true, completion: nil)
If the login proceeds without error, the above code will be called to display the screen when the login is completed.
If the flow proceeds as the code above, this screen appears.
The screen shown is not fullscreen, and the tabbar at the bottom is gone. The screen I want is the screen below.
How can I present a tab bar when presenting the screen?

Related

How to display the tabBar on the screen after the transition

I'm a beginner developing iOS apps using swift in Xcode.
[embarrassment]
I would like to know how to display tabBar on the screen after the transition by button.
If you press the button in FistViewController in the image below, you will go to nextViewController on the right side. I would like to know how to display the tabBar in nextViewController as well. (If you press the button to transition through the screen, the nextViewController will not display the tabBar.)
When you present a view controller modally, its presentation style will be "Full Screen" by default.What you need to do is change your modalPresentationStyle
guard let VC = self.storyboard?.instantiateViewController(withIdentifier: "yourVCName") else {
return
}
self.definesPresentationContext = true
VC.modalPresentationStyle = .overCurrentContext
self.present(VC, animated: true, completion: nil)

How to present unique UITabBarControllers in Swift

I'm working on a project that has two different UITabBarControllers to represent two different states of the app. I can set the first UITabBarController once a user logs in and present the second when a button is pressed. However, I'm getting odd behavior when navigating within the second UITabBarController.
This is how I set the main tab bar.
let mainTabBar = MainTabBarController()
let mainMode = UINavigationController(rootViewController: mainTabBar)
UIApplication.shared.keyWindow?.rootViewController = mainMode
I use an identical method to navigate to the second tab bar.
let secondaryTabBar = SecondaryTabBarController()
let hiddenMode = UINavigationController(rootViewController: secondaryTabBar)
UIApplication.shared.keyWindow?.rootViewController = hiddenMode
However, when using the secondary UITabBarController, I see views from the main UITabBarController when navigating to an AVCaptureSession. More specifically, I see the last view (from which the secondaryTabBar is set) from the mainTabBar under the modal presentation of the capture session. Here's the problem point:
let captureSession = CameraViewController()
navigationController?.present(captureSession, animated: true, completion: nil)
I changed the modalPresentationStyle of the CameraViewController to .overCurrentContext and that solved the issue. Got it from here: Transparent background for modally presented viewcontroller

Cant dismiss view controller after present it

After presenting a Tab bar controller, I can't dismiss tab bar controller.
I also can't even tap my button after I reinstall without delete the app. Need to uninstall and reinstall the app then I am able to tap the button
I already tried some other way of dismiss the tab bar controller but still unable to dismiss the controller.
This is my current way to present controller after login
let storyboard = UIStoryboard.init(name: "Main", bundle: Bundle.main)
let loginVC = storyboard.instantiateViewController(withIdentifier: "Home")
self.present(loginVC, animated: true, completion: nil)
This is my current way to dismiss controller
#IBAction func btnLogout_TouchUpInside(_ sender: Any) {
dismiss(animated: true, completion: nil)
}
This is my root view
let storyboard = UIStoryboard(name: "Main", bundle: Bundle.main)
if defaults.bool(forKey: "isLoggedIn") {
// Show Home screen
window?.rootViewController = storyboard.instantiateViewController(withIdentifier: "Home")
} else {
// Show Login screen
window?.rootViewController = storyboard.instantiateViewController(withIdentifier: "Login")
}
window?.makeKeyAndVisible()
try to dismiss all presented controller it may work for you
DispatchQueue.main.async {
self.view.window!.rootViewController?.dismiss(animated: true, completion: {
print("All controller dismissed successfully..")
})
}
Try This
self.presentingViewController?.dismiss(animated: true, completion: nil)
Hmm. Your update helps although I'm still not sure what's happening. My guess is that you are setting login as root, then presenting home. But I'm not sure where or what you are trying to dismiss. If it's on the login then there is nothing to dismiss as it's the root view controller.
My suggestion would be to reconfigure the storyboard. Because you are manually presenting the view controllers I'm going to assume that the storyboard doesn't contain any segues between the controllers. I'd suggest adding the segues and using them.
I'd set the home view controller as the initial view controller and set the Main storyboard as the storyboard to load in the apps settings. Then all the code to load the storyboard and set the root view controller can be removed.
Next I'd make a manual modal segue from the home view controller to the login view controller. Then in the viewDidAppear of the home view controller I'd add the code to decide if a login was needed and to them perform the login segue.
Then in the login view controller you can do a dismiss and it will remove the model login view.
This is but one of many ways to do this, but it's pretty much the simplest to get you going. You don't need any code to load or set root view controllers or anything else. It just lets the storyboard do the work for you.

Tab bar disappeared when back to initial view controller

i have 2 view controllers then tab bar controller connected to 3 navigation controller , each navigation connected to multiple views .
The point that from one of these views that is connected to one of navigation controllers , i want to back again to the start screen , it good by this 2 lines :
let loginViewController = self.storyboard?.instantiateViewController(withIdentifier: "LoginViewController")
UIApplication.shared.keyWindow?.rootViewController = loginViewController
but after i do this and want to navigate again to the tab bar its disappeared from the view . I used this to navigate to tab bar :
let appDelegate = UIApplication.shared.delegate as! AppDelegate
let viewTabController = self.storyboard?.instantiateViewController(withIdentifier :"MainTabBarController") as! MainTabBarController
viewTabController.selectedIndex = 1
appDelegate.window?.rootViewController = viewTabController
Thanks in advance.
When you use instantiateViewController you are making new instances of view controllers in memory while there is no need in your case , you have created them before. so you can dissmis your tabBar and go back to first screen like this (initial viewcontroller must be embeded into a navigation controller) :
let appDelegate = UIApplication.shared.delegate as! AppDelegate
let navigationController = appDelegate.window?.rootViewController as! UINavigationController
navigationController.dismiss(animated: true, completion: nil)
for bringing back tabBar you can either save its instance in your app delegate and present it or use a storyboard segue to present it modally
:)

Attempt to present View Controller which is not in window hierarchy

My initial view controller is created from Storyboard. It has a button which adds a UINavigation :
let navigationController = UINavigationController(rootViewController: ListViewController)
self.presentViewController(navigationController, animated: true, completion:nil)
appDelegate.window?.rootViewController = navigationController
ListViewController has a UICollectionView. Tapping on cell (which I subclassed) calls a delegate method back to ListViewController and presents another View with UICollectionView:
let detailedListViewController = DetailedCollectionViewController(collectionViewLayout: UICollectionViewFlowLayout())
self.navigationController?.pushViewController(detailedListViewController, animated: true)
Inside this DetailedCollectionView each cell has a button to show a full-screen SFSafariViewController. So as before, using delegate I'm trying to show it like this:
SFSafariVC = SFSafariViewController(URL: urlToLoad)
SFSafariVC.view.frame = self.view.frame
self.presentViewController(SFSafariVC, animated: true, completion: nil)
By doing so, I can see the SFSafariView working correctly, but I get the message:
Warning: Attempt to present < UIAlertController: 0x7fcd96353e20 > on
< UINavigationController: 0x7fcd948a0600 > whose view is not in the
window hierarchy!
I tried changing self.presentViewController to self.navigationController?.presentViewController but still the same error persists..