注意:初始答案是以编程方式创建tabBar。但是由于Josep(提问者)已经通过Storyboard创建了tabBar,我在这里更改了答案以适应需求。
假设:
(1)的TabBar使用UITabBarViewController创建。类名称:TabBarViewController
(2)最初,Tabbar由WeekendVC,WeekdayVC,OtherVC组成。 (3)基于条件,Tabbar将变为:(WeekendVC和OtherVC)或仅(WeekdayVC和OtherVC)。
这里是TabBarViewController将是什么样子:
import UIKit
enum TypeOfDay {
case weekday
case weekend
}
class TabBarViewController: UITabBarController {
var typeOfDay: TypeOfDay = .weekday
override func viewDidLoad() {
super.viewDidLoad()
// Initial order is: WeekendVC, WeekdayVC, OtherVC
if let currentViewControllers = self.viewControllers {
let weekendVC = currentViewControllers[0]
let weekdayVC = currentViewControllers[1]
let otherVC = currentViewControllers[2]
switch typeOfDay {
case .weekend:
self.viewControllers = [weekendVC, otherVC]
case .weekday:
self.viewControllers = [weekdayVC, otherVC]
}
}
}
}
如果您满意的答复,请接受并给予好评的。如果您还有其他问题,请添加评论。 – Wismin
非常感谢你酒酒,那是我最后的选择。关键是我正在使用这个项目6个月,我的标签栏是由故事板制作的,有什么办法可以添加最少的代码行吗?或者我必须再次执行我的代码?如果第二个是,我应该在我的代码中更改什么?另一件事,如果我的第一个视图控制器是我必须切换的控制器呢? –
Josep,我已经更新了答案以满足您的要求。你可以按照你想要的顺序安排。请不要忘记接受并提出答案。 – Wismin