0

I have used a custom UITabbar for my app Since my app requires a viewcontroller instead of uitabbarcontroller. In that, I have showed both the viewcontroller by adding and removing the 2nd viewcontroller as subview. Till this its fine but the tabbar item not showing the first tabbar item in highlighted state, Instead after selecting the second tabbar item(From this the tabbar items shows the highlight.)the highlighting follows.

So I tried one solution: I have added a bool variable "selected" as User Defined Runtime attribute, this also failed by showing the first tabbar item still highlighted even after the second tabbar is selected.

May I know any other alternative to show the default tab highlight when the view is loaded.

func tabBar(_ tabBar: UITabBar, didSelect item: UITabBarItem){
        switch item.tag {
        case 1:
            if sampleTwo == nil {
                var storyboard = UIStoryboard(name: "Main", bundle: nil)
                sampleTwo = storyboard.instantiateViewController(withIdentifier: "liveeventsVC") as! LiveEventsViewController
            }
            sampleTwo?.view.removeFromSuperview()

            sampleTwo?.removeFromParentViewController()
            break
        case 2:
            if sampleTwo == nil {
                var storyboard = UIStoryboard(name: "Main", bundle: nil)
                sampleTwo = storyboard.instantiateViewController(withIdentifier: "liveeventsVC") as! LiveEventsViewController
            }
            self.view.insertSubview(sampleTwo!.view!, belowSubview: self.bidLiveTabbar)

            self.addChildViewController(sampleTwo!)
            break
        default:
            break
        }
    }

How to show default tab selected while loading the screen that too in custom Tabbar?

RajeshKumar R
  • 15,445
  • 2
  • 38
  • 70
Triambagan
  • 13
  • 3
  • You dont need to make Highlight of default tab... When you show tabbarController ... it auto highlight the default Tab-bar item ... May be you have not set any Image Or Title of tab-bar item... – Ahtazaz Apr 11 '19 at 09:52
  • I am not using tabbar controller , i m using custom UITabbar . Which is dragged from the object library. – Triambagan Apr 11 '19 at 09:57
  • Sorry, I didn't notice first... – Ahtazaz Apr 11 '19 at 10:17

1 Answers1

0
IBOutlet weak var tabBar:UITabBar!//and connect this property to the UITabBar in Interface Builder


override func viewWillAppear(animated: Bool) { 
    super.viewWillAppear(animated)      

    if let items = tabBar!.items  {
            for item in items {
                if item.title == "My Tab Title" {
                    tabBar.selectedItem = item;
                }
            }
        }     
}

This works if all tabs have a unique title, which is normally the case.

Ahtazaz
  • 903
  • 8
  • 21