Programmatically change tabBar item?

浪尽此生 提交于 2019-12-05 08:14:48
Sasha Kozachuk

Check if user is logged in and then change view controllers of UITabBarController:

tabbarController.viewControllers?.replaceRange()

Also this may help Set view controllers of UITabBarController in Swift

You need to replace second tab associated viewcontroller by a new viewcontroller. here is sample code, which may help you:

NSMutableArray *viewControllers = [[NSMutableArray alloc] initWithArray:self.tabBarController.viewControllers];
UIViewController *newVC = [UIViewController new];
UINavigationController *newNav = [[UINavigationController alloc] initWithRootViewController:newVC];
[viewControllers replaceObjectAtIndex:1 withObject:newNav];
self.tabBarController.viewControllers = viewControllers 

Try this:

self.tabBar.selectedIndex = 0

If you want change of viewController

Below is the code for Swift

        let tabViewCntrls : NSMutableArray = ((self.tabBarController?.viewControllers)! as NSArray).mutableCopy() as! NSMutableArray

        let vcLogin = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "LoginScreen") as! LoginScreen

        let navRootVC = UINavigationController.init(rootViewController: vcLogin)

        navRootVC.viewControllers = [vcLogin]

        tabViewCntrls.replaceObject(at: 1, with: navRootVC)

        self.tabBarController?.viewControllers = tabViewCntrls as? [UIViewController]
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!