我可以在同一View Controller中添加两个选项卡栏吗,底部一个选项卡栏,顶部一个选项卡栏

时间:2018-11-28 09:32:16

标签: ios iphone uitabbarcontroller xcode10 swift4.2

我正在用Swift 4开发一个应用程序,我想同时添加两个标签栏,一个在底部,另一个在顶部。我已经添加了底部标签栏,但不知道如何在同一控制器中添加第二个标签栏

谢谢

1 个答案:

答案 0 :(得分:0)

是的,你可以做。如下图所示,在底部的UITabBar上添加另一个,在顶部添加另一个。

但是,您只能在每个UITabBar中选择一项。例如,如果您在顶部标签栏中选择了Contacts,则也可以在底部标签栏中选择Favourites。如果要在两个选项卡栏中选择一项,则应手动进行编程。

将代码添加到viewDidLoad

let tabbar1 = UITabBar() //Note that tabbar height is fixed to 49
view.addSubview(tabbar1)

tabbar1.translatesAutoresizingMaskIntoConstraints = false
tabbar1.leadingAnchor.constraint(equalTo: view.leadingAnchor, constant: 0.0).isActive = true
tabbar1.trailingAnchor.constraint(equalTo: view.trailingAnchor, constant: 0.0).isActive = true
tabbar1.topAnchor.constraint(equalTo: view.safeAreaLayoutGuide.topAnchor, constant: 0.0).isActive = true

let contacts = UITabBarItem(tabBarSystemItem: UITabBarItem.SystemItem.contacts, tag: 100)
let bookmarks = UITabBarItem(tabBarSystemItem: UITabBarItem.SystemItem.bookmarks, tag: 101)
tabbar1.setItems([contacts, bookmarks], animated: false)

let tabbar2 = UITabBar() //Note that tabbar height is fixed to 49
view.addSubview(tabbar2)

tabbar2.translatesAutoresizingMaskIntoConstraints = false
tabbar2.leadingAnchor.constraint(equalTo: view.leadingAnchor, constant: 0.0).isActive = true
tabbar2.trailingAnchor.constraint(equalTo: view.trailingAnchor, constant: 0.0).isActive = true
tabbar2.bottomAnchor.constraint(equalTo: view.safeAreaLayoutGuide.bottomAnchor, constant: 0.0).isActive = true

let downloads = UITabBarItem(tabBarSystemItem: UITabBarItem.SystemItem.downloads, tag: 103)
let favorites = UITabBarItem(tabBarSystemItem: UITabBarItem.SystemItem.favorites, tag: 104)
tabbar2.setItems([downloads, favorites], animated: false)

enter image description here