【问题标题】:Is there a way to remove of add a tab to a tab bar dynamically while using the app in iOS?在 iOS 中使用应用程序时,有没有办法动态删除将选项卡添加到选项卡栏?
【发布时间】:2013-05-10 00:02:47
【问题描述】:
我的应用中有一个标签栏,上面有 5 个标签。最后一个标签显示一些广告。我想添加一个设置,用户可以将第 5 个选项卡“关闭”....所以基本上我只是将它从屏幕上删除。
请注意,我不想隐藏它,我想删除它,以便剩余的 4 个选项卡自动均匀间隔。
我想通过重新添加标签来做同样的事情。
是否可以在用户不必重新启动应用的情况下执行此操作?
【问题讨论】:
标签:
iphone
ios
objective-c
uitabbarcontroller
uitabbar
【解决方案1】:
您只需要使用 UITabBarController 的 viewControllers 属性即可。
使用此代码删除最后一个视图控制器:
NSMutableArray *mutableViewControllers = [tabBarController.viewControllers mutableCopy];
[mutableViewControllers removeLastObject];
tabBarController.viewControllers = mutableViewControllers;
在需要时使用此代码恢复最后一个视图控制器:
NSMutableArray *mutableViewControllers = [tabBarController.viewControllers mutableCopy];
[mutableViewControllers addObject:previouslyRemovedViewController];
tabBarController.viewControllers = mutableViewControllers;
当然,这个例子假设你有一个 tabBarController 的引用,并且你保留了你的 lastViewController(例如在一个属性上)。
另外,请确保在主线程上运行此代码。