简体   繁体   English

如何为UITabBarController中包含两次的同一情节提要板设置不同的UITabBarItems?

[英]How can I set different UITabBarItems for the same storyboard reference included twice in a UITabBarController?

I have a UITabBarController containing four view controllers. 我有一个包含四个视图控制器的UITabBarController The first two of these are storyboard references to the same external storyboard: "BookTable" in the image below. 其中的前两个是对同一外部故事板的故事板引用:下图中的“ BookTable”。

带两个子VC的标签栏控制器

I want to set different tab bar icons for these two items, but I can't work out how. 我想为这两项设置不同的标签栏图标,但我不知道如何做。 If I don't set the tab bar item properties in the embedded storyboard, then there is no tab bar present in the running app, even if I have configured the tab bar in the controller. 如果未在嵌入式情节提要中设置选项卡栏项目属性,则即使我已在控制器中配置了选项卡栏,正在运行的应用程序中也没有选项卡栏。 See the storyboard and the simulator below: 请参见下面的情节提要和模拟器:

具有指定项目的标签栏控制器缺少项目的模拟器屏幕截图

If I set the tab bar item properties in the embedded storyboard, then the tab bar item is the same for both: 如果我在嵌入式情节提要板中设置了选项卡栏项目属性,则两个选项卡栏项目都相同:

带重复项的标签栏控制器

If I set the tab bar item properties in code, nothing changes: 如果我在代码中设置了标签栏项目属性,则不会发生任何变化:

class TabBarController: UITabBarController {

    override func viewDidLoad() {
        super.viewDidLoad()

        tabBar.items![0].title = "Test Item 1"
        tabBar.items![1].title = "Test Item 1"
    }

}

I there a way to do this, or will I have to duplicate my storyboard just to set a different tab bar item? 我有办法做到这一点,还是只需要设置一个不同的选项卡栏项目就必须复制情节提要?

Unfortunately, Interface Builder does not offer too many possibilities when it comes to view controller reusing in tab bar controllers. 不幸的是,当要在标签栏控制器中重新使用视图控制器时,Interface Builder并没有提供太多的可能性。 You'll have to specify the view controllers of the UITabBarController programmatically: 您必须以编程方式指定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"
    }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 故事板中的UITabBarController隐藏了UITabBarItems的内容 - UITabBarController in storyboard hides content of UITabBarItems 如何本地化我的UITabBarItems? - How can I localize my UITabBarItems? 如何使用 StoryBoard 在 UITabBarController 中设置选定的选项卡? - How do I set selected tab in UITabBarController using StoryBoard? 如何使用Storyboard重新排列UITabBarController项目? - How can I rearrange UITabBarController items using the Storyboard? 如何从 Storyboard 将 UITabBarController 设置为 rootViewController - How to set a UITabBarController as rootViewController from Storyboard 如何从大小不同的情节提要中使用相同的ViewController? - How can I use the same ViewController from a storyboard with different sizes? 如何在没有标题的情况下为UITabBarItems设置图像? - How to set the images for UITabBarItems without having titles? iOS / xcode-如何从UITabBarController打开新的情节提要,并防止UITabBar消失? - iOS/xcode - How can I open a new storyboard from UITabBarController and keep the UITabBar from disappearing? 如何在Main.storyboard中具有参考的其他Storyboard中为导航控制器设置选项卡图标和标题? - How to set tab bar icon and title for a Navigation controller in different Storyboard which has reference in Main.storyboard? 我可以为同一个 ViewController 使用不同的故事板设计吗 - Can I use different Storyboard Design for same ViewController
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM