Swift: Gesturing doesn't bring back the TabBar - swift

I am trying to move back from a ViewController (simple text page) to a main ViewController with a TabBar at the bottom using gesturing.
The gesture works as I return to the original main screen but there is no TabBar.
In the simple ViewController I use this code to go back to the originating ViewController.
#objc func respondToSwipeGesture(gesture: UIGestureRecognizer) {
if let swipeGesture = gesture as? UISwipeGestureRecognizer {
switch swipeGesture.direction {
case UISwipeGestureRecognizerDirection.right:
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let controller = storyboard.instantiateViewController(withIdentifier: "AboutViewControllerID")
self.present(controller, animated: true, completion: nil)
if let vc = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "AboutViewControllerID") as? AboutViewController
{
present(vc, animated: true, completion: nil)
}
default:
break
}
}
}
In the ViewController with the TabBarController I have tried the following lines to rejuvinate the TabBarController but without any joy.
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(true)
tabBarController?.tabBar.isHidden = false
NSLog("TabBar true")
}
Any ideas?

First if you want to go-back don't use present as it will add the same VCS twice in the stack , you have to use unwindSegue/dismiss , or load the tabBar itself with id
self.dismiss(animated: true) {
// use this if it's not directly behind the dismissed VC
let tab = storyboard.instantiateViewController(withIdentifier: "tabBarID") as! UITabBarController
UIApplication.shared.keyWindow?.rootViewController = tab
}
//

Related

Navigation bar is not seen while using present to view another view controller

I am not able to see the navigation bar in a view controller while presenting it from another view controller
I have tried keeping
self.navigationController?.navigationBar.isHidden = false
and also tried keeping a navigation controller to the view controller(embedded in).
Controller A:-
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let viewController =
storyboard.instantiateViewController(withIdentifier:"Identifier") as! B
present(viewController, animated: true, completion: nil)
controller B:-
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
self.navigationController?.navigationBar.isHidden = false
}
Try this
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let ViewController =
storyboard.instantiateViewController(withIdentifier:"Identifier") as! B
let vc = UINavigationController(rootViewController: ViewController)
present(vc, animated: true, completion: nil)
If you are doing it programmatically then it will help you:-
SWIFT 4
let vc = UIStoryboard.init(name: "Main", bundle: Bundle.main).instantiateViewController(withIdentifier: "IKDetailVC") as? IKDetailVC
self.navigationController?.pushViewController(vc!, animated: true)
In the first view controller viewWillAppear(), add this:
override func viewWillAppear(animated: Bool) {
self.navigationController?.navigationBarHidden = true
}
In the second one, add this:
override func viewWillAppear(animated: Bool) {
self.navigationController?.navigationBarHidden = false
}
override func viewDidDisappear(animated: Bool) {
self.navigationController?.navigationBarHidden = true
}
Happy Coding😊

How can i return to Main.storyboard which contains a TabBarController from another Storyboard

I have two storyboards, one which contains everything the app really is, and another which contains an "onboarding"/tutorial to my app.
Once the tutorial is done, I want to navigate back to my original view controller.
This is my code to navigate to the other storyboard:
let defaults = UserDefaults.standard
if defaults.bool(forKey: "firstOpened") {
print("First VC launched")
}else{
var vc: UIViewController
let goTo = UIStoryboard(name: "Onboarding", bundle: nil).instantiateViewController(withIdentifier: "notificationStoryboard") as! FirstOnboardingViewController
self.present(goTo, animated: true, completion: nil)
}
With this, it works, except the TabBarController is not showing, and not working the way I want it to.
And this is my code to navigate back to the Main.Storyboard:
#objc func handleSecondPush() {
//registerForRemoteNotifications()
UserDefaults.standard.set(true, forKey: "firstOpened")
let goTo = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "pushToFeedVC")
self.present(goTo, animated: true, completion: nil)
//self.performSegue(withIdentifier: "goToLink", sender: nil)
}
I have also tried this in the other Storyboard, but with this the button doesn't change the view:
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let controller = storyboard.instantiateViewController(withIdentifier: "MainTabBarController") as! UITabBarController
print(controller)
self.window?.rootViewController = controller
self.window?.makeKeyAndVisible()
if let tabBarVc = self.window?.rootViewController as? UITabBarController {
tabBarVc.selectedIndex = 1
}
Question in short terms: So my question is, how can I navigate back to the main.storyboard which will contain the TabBarController with a selected index of 1, from a storyboard that doesn't contain navigation controller or a TabBarController?
When you present the onbaording you should return back to the tab with with
self.dismiss(animated:true,completion:nil)
for complex presentations you can do this for easy re-back
let vc = storyboard.instantiateViewController(withIdentifier: "MainTabBarController") as! UITabBarController
(UIApplication.shared.delegate as! AppDelegate).window!.rootViewController = vc
What is better is to
let goTo = UIStoryboard(name: "Onboarding", bundle: nil).instantiateViewController(withIdentifier: "notificationStoryboard") as! FirstOnboardingViewController
let nav = UINavigationController(rootViewController:goTo)
nav.isNavigationBarHidden = true
self.present(nav, animated: true, completion: nil)
the flow inside the onbarding should be with pushViewController
then to dismiss in the last onbaording vc
if let tab = (UIApplication.shared.delegate as! AppDelegate).window?.rootViewController as? UITabBarController {
tab.dismiss(animated:true,completion:nil)
}

Swift - Create a menu to navigate to the same view controller from multiple places

This is my code to navigate different navigation controllers in my app.
I have a modal view to present the menu and later select where navigate,
this is a good approach?
func circleMenu(_: CircleMenu, buttonDidSelected _: UIButton, atIndex: Int) {
weak var pvc = self.presentingViewController
if atIndex == 1 {
self.dismiss(animated: true) {
let storyboard = UIStoryboard(name: "Experiencia", bundle: nil)
let vc = storyboard.instantiateViewController(withIdentifier: "ExperienciaNavigationController")
pvc?.present(vc, animated: true, completion: nil)
}
}
if atIndex == 2 {
self.dismiss(animated: true) {
let storyboard = UIStoryboard(name: "Servicios", bundle: nil)
let vc = storyboard.instantiateViewController(withIdentifier: "ServiciosNavigationController")
pvc?.present(vc, animated: true, completion: nil)
}
}
if atIndex == 3 {
self.dismiss(animated: true) {
let storyboard = UIStoryboard(name: "Book", bundle: nil)
let vc = storyboard.instantiateViewController(withIdentifier: "DemoViewController")
pvc?.present(vc, animated: true, completion: nil)
}
}
}
And later in other vc that was selected I have this code to return to root
func back() {
self.view.window?.rootViewController?.dismiss(animated: true, completion: nil)
}
And the final question its about the navigation controller that will be closed... Will be closed all the views presented? to avoid memory issues.
I read about implement a coordinator pattern. but I don't know if this case to do that Coordinator pattern or Navigator

how to close a tabbar swift

I have a login view controller which on a correct login will open a tabbar controller with the following method:
self.dismissViewControllerAnimated(true, completion: { self.loadHomeScreen()})
func loadHomeScreen()
{
emailField.text = ""
passwordField.text = ""
self.presentViewController(UIStoryboard.tabbarController()!, animated: true, completion: nil)
}
private extension UIStoryboard {
class func mainStoryboard() -> UIStoryboard { return UIStoryboard(name: "Main", bundle: NSBundle.mainBundle()) }
class func tabbarController() -> UITabBarController? {
return mainStoryboard().instantiateViewControllerWithIdentifier("TabbarControllerID") as? UITabBarController
}
}
I think my login view controller is still in the background? I have a logout button as the rightbarbutton item on each navbar in my tabgroup. I want to be able to close the tabbar on this button press. How can I achieve this. I can't find any examples. Would I be using a pop command?
UPDATE:
#IBAction func tryLogout(sender: UIBarButtonItem) {
self.dismissViewControllerAnimated(true, completion: nil)
let storyboard = UIStoryboard(name: "main", bundle: nil)
let vc = storyboard.instantiateViewControllerWithIdentifier("login") as! UIViewController
self.presentViewController(vc, animated: true, completion: nil)
}

Content under a login View is visible for a fraction of second

I have a view displaying some content, which is password protected. In viewWillAppear a variable gets checked, to see if the user is properly logged in :
override func viewWillAppear(animated: Bool) {
if (!Config.userLoggedIn) {
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let loginVC = storyboard.instantiateViewControllerWithIdentifier("loginVC") as! UIViewController
loginVC.modalTransitionStyle = UIModalTransitionStyle.CrossDissolve
self.navigationController!.presentViewController(loginVC ,animated: true, completion: nil)
}
}
It works, but the content underneath is visible for a short fraction of a second. How can I present the loginVC without revealing the content underneath.
I cannot put it in viewDidLoadbecause all this is part of a TabBarController, and the views might already be in memory and viewDidLoad is only called once
Simple solution. Hide whole view in viewDidLoad then show it back in viewViewAppear if user is correctly logged in
override func viewDidLoad(){
self.view.hidden = true
}
override func viewWillAppear(animated: Bool) {
if (!Config.userLoggedIn) {
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let loginVC = storyboard.instantiateViewControllerWithIdentifier("loginVC") as! UIViewController
loginVC.modalTransitionStyle = UIModalTransitionStyle.CrossDissolve
self.navigationController!.presentViewController(loginVC ,animated: true, completion: nil)
}else{
self.view.hidden = false
}
}