Swift: how to show a tab bar controller after a login view

前端 未结 5 1095
抹茶落季
抹茶落季 2021-01-30 22:45

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 an

相关标签:
5条回答
  • 2021-01-30 23:20

    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.. :)

    0 讨论(0)
  • 2021-01-30 23:29

    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)
    
    0 讨论(0)
  • 2021-01-30 23:34

    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)
    })
    
    0 讨论(0)
  • 2021-01-30 23:34
    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)
    }
    
    0 讨论(0)
  • 2021-01-30 23:43

    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)
    
    0 讨论(0)
提交回复
热议问题