swiftxcodetabbar

How to remove and add tabbar item in swift 4


How to remove checkout bar item when the cart is empty and show again if the cart is load, also remove the space when it 3 item

[this is the 4 item]

I tried this but it removes the item from the array

  if var tabs = self.tabBarController?.viewControllers {
            tabs.remove(at: indexToRemove)
            self.tabBarController?.viewControllers = tabs
        } else {
            print("There is something wrong with tabbar 
  controller")
        }

Solution

  • Above you are removing the viewController from the tabs array, not the tabBarController's viewControllers array. (check the count of the two arrays to show this)

    All you need to do is:

     self.tabBarController.viewControllers?.remove(at: indexToRemove)
    

    This will remove the viewController from the tab bar. As an aside, depending on how the viewController was created, it may also de-initialise it so just be aware of that if you are planning to add it again later without reinitialising it. Make sure you have a strong reference to it. To add it back, just insert in back into the array.