Swift: how to show a tab bar controller after a login view
Solution 1
To show tab bar controller from Login page, connect the Login page and TabbarController with a Show segue and give it an identifier in attributes inspector (Say "mySegueIdentifier").
To add segue, just right click and drag from Login view controller to TabbarController.
In successful Login you can simply call "performSegueWithIdentifier" method as follows
self.performSegue(withIdentifier: "mySegueIdentifier", sender: nil)
In your case you call it after this line.
NSLog("Login OK")
If you don't want to navigate from Login page to TabbarController, you can also set it as rootViewController after successful Login. To do this, set an identifier to TabbarController (Say "myTabbarController")
let appDelegate = UIApplication.shared.delegate! as! AppDelegate
let initialViewController = self.storyboard!.instantiateViewController(withIdentifier: "myTabbarControllerID")
appDelegate.window?.rootViewController = initialViewController
appDelegate.window?.makeKeyAndVisible()
Edit:
Swift 3
let appDelegate = UIApplication.shared.delegate! as! AppDelegate
let initialViewController = self.storyboard!.instantiateViewController(withIdentifier: "myTabbarControllerID")
appDelegate.window?.rootViewController = initialViewController
appDelegate.window?.makeKeyAndVisible()
Happy coding.. :)
Solution 2
Give your tab bar controller a StoryboardID (say "tabbar") and push it just like a normal UIViewController:
let nextViewController = self.storyboard?.instantiateViewController(withIdentifier: "tabbar") as! UIViewController
self.navigationController?.pushViewController(nextViewController, animated: true)
Solution 3
I ran into this same issue when trying to segue from a controller I used for touchID to a TabBarController. By making the segue in an async block I resolved the issue.
dispatch_async(dispatch_get_main_queue(), {
self.dismissViewControllerAnimated(false, completion: {})
self.performSegueWithIdentifier("authnToAppSegue", sender: nil)
})
Solution 4
Call the Tabbarcontroller from any controller By below Code, default first item is selected
let storyBoard : UIStoryboard = UIStoryboard(name: "Main", bundle:nil)
let nextViewController = storyBoard.instantiateViewController(withIdentifier: "HomeTabBar") as! UITabBarController
self.navigationController?.pushViewController(nextViewController, animated: true)
Solution 5
func setTabBarVisible(visible:Bool, animated:Bool) {
//* This cannot be called before viewDidLayoutSubviews(), because the frame is not set before this time
// bail if the current state matches the desired state
if (tabBarIsVisible() == visible) { return }
// get a frame calculation ready
let frame = self.tabBarController?.tabBar.frame
let height = frame?.size.height
let offsetY = (visible ? -height! : height)
// zero duration means no animation
let duration:NSTimeInterval = (animated ? 0.3 : 0.0)
// animate the tabBar
if frame != nil {
UIView.animateWithDuration(duration) {
self.tabBarController?.tabBar.frame = CGRectOffset(frame!, 0, offsetY!)
return
}
}
}
func tabBarIsVisible() ->Bool {
return self.tabBarController?.tabBar.frame.origin.y < CGRectGetMaxY(self.view.frame)
}
// Call the function from tap gesture recognizer added to your view (or button)
@IBAction func tapped(sender: AnyObject) {
setTabBarVisible(!tabBarIsVisible(), animated: true)
}
Related videos on Youtube
SagittariusA
Updated on April 04, 2022Comments
-
SagittariusA about 2 years
I have seen many posts similar to this here but they are all about Objective-C while I am developing my app in Swift. As you can see from the image I have a login screen view and I correctly implemented the login mechanism.
Now I would like that when the login has succedeed, then the tab bar controller is showed. In my login view controller I have this function for login:
var finalURL:NSString = "\(Settings.webServerLoginURL)?username=\(username)&password=\(password)" LoginService.requestLoginWithURL(NSURL(string: finalURL as String)!, completionHandler: { (success) -> Void in if (success) { NSLog("Login OK") /* Scarica dal database i tasks di LoggedUser.id */ /* Redirect al tab HOME dell'applicazione dove si mostrano il numero di task di quell'utente ed in cima "BENVENUTO: name surname" */ } else { self.alertView.title = "Autenticazione fallita!" self.alertView.message = "Username o passowrd." self.alertView.delegate = self self.alertView.addButtonWithTitle("OK") self.alertView.show() }
So I think that I should show tab bar controller after
NSLog("Login OK")
but I don't know how. I am a beginner of Swift/XCode...if you can explain me. Thanks to all who have read.
-
Ajay Singh Thakur about 8 yearshere is another way -stackoverflow.com/questions/36517047/…
-
-
SagittariusA over 8 yearsThank you for you answer. I am afraid I don't understand... I have added those functions and I called setTabBarVisible(!tabBarIsVisible(), animated: true) after the login has succeeded but nothing happens... Maybe because there is no link between login view controller and tab bar controller in the story board?
-
Kristijan Delivuk over 8 yearsmake one navigation controller and stack your views in it ... view isn't owner of navigation items but navigation controller is ...
-
Kristijan Delivuk over 8 yearshere is good tutorial on doing this makeapppie.com/2014/09/09/…
-
SagittariusA over 8 yearsthank you. I will have a look on that tutorial...I don't really know where to start from to develop this application...
-
SagittariusA over 8 yearsThank you, this was really enlightening and simple. It works. Just another thing: when the view switches to tab bar controller, I see only the first tab icon and title (home) as enabled. I would like too see also the other tabs as not enabled but this happens only if I click on "them" (on the blank space where they are supposed to be)...can this be fixed?
-
Kristijan Delivuk over 8 yearsu need to understand difference and link between navigation controller and view controller because nav controller is the owner of toolbar (top bar) and tab bar (bottom bar) so if u want to use them in your application u need to make stack with navigation controller in which then u put other view controllers (nav controller is a stack on which u push and pop view controllers)
-
Dev over 8 yearsThat is the default behavior of the Tab bar. Based on this only user can know that which page he is in. You can observe this in so many apps.
-
SagittariusA over 8 yearsWait, maybe I haven't explained well the things...the fact is that the other icons don't appear at all. For example if I am in the Home tab I only see the Home icon while the others are not showed so a user wouldn't know where to tap... Right?
-
Dev over 8 yearsMight be you forgotten to put the images to tab bar items for "selectedImage" property --> tabBarItem.selectedImage = UIImage(named: "tab-active.png")?.imageWithRenderingMode(UIImageRenderingMode.AlwaysOriginal).
-
SagittariusA over 8 yearsEhm...yes, I put the images for the icons. I uploaded them in XCode and set the right image for the right tab through the storyboard. I mean that I click on the correspondent view, I click on the icon and select the image on the inspector.....
-
SagittariusA over 8 yearsOk, solved...I think it's a problem with the iOS simulator because if I reset it then this problem disappears...
-
SagittariusA over 8 yearsforgive me if I am annoying you again, there is something strange happening...even if the login procedure fails or even if I try lo login without password or username (or without both) after the login button is pressed then the the screen is switched to tab bar controller... I just inserted self.performSegueWithIdentifier("mySegueIdentifier", sender: nil) after only if success = true (in my code in my question) but it showed anyway...why? :(
-
Dev over 8 yearsI edited my answer. Check it once. In your case you have to give segue between login view controller and tab bar controller. Not between login button and tab bar controller.
-
Admin about 2 yearsAs it’s currently written, your answer is unclear. Please edit to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers in the help center.