Ios 如何在swift 3中动态隐藏tabBarController中的选项卡
我正在使用UITabBarController,并使用故事板中的关系序列添加了选项卡Ios 如何在swift 3中动态隐藏tabBarController中的选项卡,ios,swift,uitabbarcontroller,uitabbar,ios11,Ios,Swift,Uitabbarcontroller,Uitabbar,Ios11,我正在使用UITabBarController,并使用故事板中的关系序列添加了选项卡 如何根据登录的用户角色隐藏特定选项卡?如果要从选项卡栏控制器中删除选项卡,请执行类似操作(当用户未登录时) 当您的用户登录时 UIViewController *viewController = [[UIViewController alloc] init]; NSMutableArray *tabs = [NSMutableArray arrayWithArray:self.tabBarController.
如何根据登录的用户角色隐藏特定选项卡?如果要从选项卡栏控制器中删除选项卡,请执行类似操作(当用户未登录时) 当您的用户登录时
UIViewController *viewController = [[UIViewController alloc] init];
NSMutableArray *tabs = [NSMutableArray arrayWithArray:self.tabBarController.viewControllers];
[tabs addObject:viewController];
self.tabBarController.viewControllers = tabs;
快速版本
删除选项卡
let indexToRemove = 0
if var tabs = self.tabBarController?.viewControllers {
tabs.remove(at: indexToRemove)
self.tabBarController?.viewControllers = tabs
} else {
print("There is something wrong with tabbar controller")
}
let indexToAdd = 2
let vc = UIViewController.init()
if var tabs = self.tabBarController?.viewControllers {
tabs.append(vc) // Append at last index of array
// tabs.insert(vc, at: indexToAdd) // Insert at specific index
self.tabBarController?.viewControllers = tabs
} else {
print("There is something wrong with tabbar controller")
}
添加选项卡
let indexToRemove = 0
if var tabs = self.tabBarController?.viewControllers {
tabs.remove(at: indexToRemove)
self.tabBarController?.viewControllers = tabs
} else {
print("There is something wrong with tabbar controller")
}
let indexToAdd = 2
let vc = UIViewController.init()
if var tabs = self.tabBarController?.viewControllers {
tabs.append(vc) // Append at last index of array
// tabs.insert(vc, at: indexToAdd) // Insert at specific index
self.tabBarController?.viewControllers = tabs
} else {
print("There is something wrong with tabbar controller")
}
问得好
您需要挖掘及其成员(属性+函数)
现在,请关注以下步骤以找到解决方案:
viewControllers
(它是UIViewController的数组),用于存储使用UITabbar项关联的选项卡栏控制器的UIViewControllersViewController
属性func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
if var tabController = self.window?.rootViewController as? UITabbarController, var viewControllers = tabController.viewControllers {
let isLoggedIn = <get value from your data storage as bool>
if isLoggedIn {
viewControllers.remove(at: firstIndex) // By considering you need to remove view controller at index first. It will automatically remove tab from tabbar also.
tabController.viewControllers = viewControllers
self.window?.rootViewController = tabController
// further operations to make your root controller visible....
}
}
}
func应用程序(application:UIApplication,didFinishLaunchingWithOptions launchOptions:[UIApplicationLaunchOptions:[UIApplicationLaunchOptions:Any]?)->Bool{
如果var tabController=self.window?.rootViewController为UITabbarController,则var viewControllers=tabController.viewControllers{
让isLoggedIn=
如果伊斯洛格丁{
viewControllers.remove(at:firstIndex)//考虑到需要首先删除索引处的视图控制器。它还会自动从选项卡栏中删除选项卡。
tabController.viewControllers=viewControllers
self.window?.rootViewController=tabController
//使根控制器可见的进一步操作。。。。
}
}
}
你能详细说明一下这个问题吗?您可能还需要添加一段代码片段,介绍您迄今为止尝试的内容……如何初始化窗口?对不起,我是ios新手,[也引用了此链接,但它在我的代码-self.window中不起作用?
是AppDelegate
的属性。您不需要初始化它。只需从序列图像板创建tabbar控制器。将主序列图像板和根控件作为tabbar从序列图像板分配给您的项目。此代码自动起作用。我已登录为根控制器。用户成功登录,然后TabBarController打开。我已经从情节提要中添加了TabBarController中的所有选项卡,并且工作正常。现在,我想根据用户角色(我在登录响应中从服务器获得)服务器响应-“featuresAction”:“[{”Frature1\”:这是一个响应,我想根据功能列表(我从服务器获取)隐藏选项卡TabBarControllers:.//Frature2//Frature4您能告诉我是什么问题导致的吗?在remove tab code中,在第2行获取错误作为EXC_BAD_指令(EXC_1386_INVOP,子代码=0*0)@ShubhangiShedage请检查我的编辑,很抱歉我没有测试我的代码。@PratikJamariya,亲爱的,不要强行打开tabbar controller的ViewController。这可能会导致应用程序崩溃(self.tabbar controller?.ViewController)!
。如果让应用程序崩溃,请尝试。@Krunal请随意编辑我的答案,我不擅长swift,但我的Objective-C技能非常好。