如何为 UITabBarController 中包含两次的同一故事板参考设置不同的 UITabBarItems?
How can I set different UITabBarItems for the same storyboard reference included twice in a UITabBarController?
我有一个 UITabBarController
包含四个视图控制器。其中前两个是对相同外部故事板的故事板引用:下图中的"BookTable"。
我想为这两项设置不同的标签栏图标,但我不知道如何设置。如果我没有在嵌入式故事板中设置选项卡栏项目属性,那么 运行 应用程序中不会出现 no 选项卡栏,即使我已经配置了选项卡栏在控制器中。请参阅下面的故事板和模拟器:
如果我在嵌入式情节提要中设置标签栏项目属性,那么两者的标签栏项目是相同的:
如果我在代码中设置选项卡栏项目属性,则没有任何变化:
class TabBarController: UITabBarController {
override func viewDidLoad() {
super.viewDidLoad()
tabBar.items![0].title = "Test Item 1"
tabBar.items![1].title = "Test Item 1"
}
}
我有办法做到这一点,还是我必须复制我的故事板才能设置不同的标签栏项目?
不幸的是,Interface Builder 在选项卡栏控制器中重用视图控制器时并没有提供太多可能性。您必须以编程方式指定 UITabBarController
的视图控制器:
class TabBarController: UITabBarController {
override init(nibName nibNameOrNil: String?, bundle nibBundleOrNil: Bundle?) {
super.init(nibName: nibNameOrNil, bundle: nibBundleOrNil)
setUp()
}
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
setUp()
}
func setUp() {
let storyboard = UIStoryboard(name: [storyboard id], bundle: nil)
let firstBookTableVc = storyboard.instantiateViewController(withIdentifier: "BookTable")
let secondBookTableVc = storyboard.instantiateViewController(withIdentifier: "BookTable")
//configure the view controllers here...
viewControllers = [firstBookTableVc, secondBookTableVc]
tabBar.items?[0].image = [first tab bar image]
tabBar.items?[1].image = [second tab bar image]
tabBar.items?[0].title = "first title"
tabBar.items?[1].title = "second title"
}
}
我有一个 UITabBarController
包含四个视图控制器。其中前两个是对相同外部故事板的故事板引用:下图中的"BookTable"。
我想为这两项设置不同的标签栏图标,但我不知道如何设置。如果我没有在嵌入式故事板中设置选项卡栏项目属性,那么 运行 应用程序中不会出现 no 选项卡栏,即使我已经配置了选项卡栏在控制器中。请参阅下面的故事板和模拟器:
如果我在嵌入式情节提要中设置标签栏项目属性,那么两者的标签栏项目是相同的:
如果我在代码中设置选项卡栏项目属性,则没有任何变化:
class TabBarController: UITabBarController {
override func viewDidLoad() {
super.viewDidLoad()
tabBar.items![0].title = "Test Item 1"
tabBar.items![1].title = "Test Item 1"
}
}
我有办法做到这一点,还是我必须复制我的故事板才能设置不同的标签栏项目?
不幸的是,Interface Builder 在选项卡栏控制器中重用视图控制器时并没有提供太多可能性。您必须以编程方式指定 UITabBarController
的视图控制器:
class TabBarController: UITabBarController {
override init(nibName nibNameOrNil: String?, bundle nibBundleOrNil: Bundle?) {
super.init(nibName: nibNameOrNil, bundle: nibBundleOrNil)
setUp()
}
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
setUp()
}
func setUp() {
let storyboard = UIStoryboard(name: [storyboard id], bundle: nil)
let firstBookTableVc = storyboard.instantiateViewController(withIdentifier: "BookTable")
let secondBookTableVc = storyboard.instantiateViewController(withIdentifier: "BookTable")
//configure the view controllers here...
viewControllers = [firstBookTableVc, secondBookTableVc]
tabBar.items?[0].image = [first tab bar image]
tabBar.items?[1].image = [second tab bar image]
tabBar.items?[0].title = "first title"
tabBar.items?[1].title = "second title"
}
}