Search code examples
iosiphoneuitabbarcontrollerxcode10swift4.2

Can i Add Two Tab Bars in same View controller one tab bar at bottom and one tab bar at top


I am developing an app in swift 4 and i want to add two tab bars at same time one at bottom and other at top. i have added bottom tab bar but have no idea how to add second tab bar in the same controller

thanks


Solution

  • Yes, you can do. Add on UITabBar at bottom and another at top like shown below.

    However, you can select one item in each UITabBar once. For example, if you selected Contacts in top tabbar, you can also select Favourites at the bottom tabbar as well. If you want to select one item in both tabbars you should program manually.

    Add the code to 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