我可以在同一个视图控制器中添加两个标签栏吗?一个标签栏在底部,一个标签栏在顶部
Can i Add Two Tab Bars in same View controller one tab bar at bottom and one tab bar at top
我正在 swift 4 中开发一个应用程序,我想同时添加两个标签栏,一个在底部,另一个在顶部。我添加了底部标签栏,但不知道如何在同一个控制器中添加第二个标签栏
谢谢
是的,你可以做到。在底部添加 UITabBar
,在顶部添加另一个,如下所示。
但是,您可以 select 每个 UITabBar
一次。例如,如果您在顶部标签栏中 selected Contacts
,您也可以在底部标签栏中 select Favourites
。如果你想 select 两个标签栏中的一项,你应该手动编程。
将代码添加到 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)
我正在 swift 4 中开发一个应用程序,我想同时添加两个标签栏,一个在底部,另一个在顶部。我添加了底部标签栏,但不知道如何在同一个控制器中添加第二个标签栏
谢谢
是的,你可以做到。在底部添加 UITabBar
,在顶部添加另一个,如下所示。
但是,您可以 select 每个 UITabBar
一次。例如,如果您在顶部标签栏中 selected Contacts
,您也可以在底部标签栏中 select Favourites
。如果你想 select 两个标签栏中的一项,你应该手动编程。
将代码添加到 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)