开发者

How to set first TabBar selected programmatically on iPhone

开发者 https://www.devze.com 2023-01-02 05:51 出处:网络
I have UITabBar in view which have 5 tabs. I am using didSelectItem delegate to open different view i.e. I am NOT using TabBarController.

I have UITabBar in view which have 5 tabs. I am using didSelectItem delegate to open different view i.e. I am NOT using TabBarController.

My problem is on view load I need first tab get selected by default. Is there any property in TabBar which we can set to make i开发者_如何学编程t selected?

Thanks.


This code will work [tabBar setSelectedItem:[tabBar.items objectAtIndex:0]];


In swift if tabbar is used not tabbarcontroller set default select

var tabbar:CustomTabBar?//if declare like this
tabbar!.selectedItem = self.tabbar!.items![0] as? UITabBarItem

or

let tabbar = UITabBar()//if declare and initilize like this
tabbar.selectedItem = self.tabbar.items![0] as? UITabBarItem


set the tabbar.selectedItem=0; in the viewWillAppear so when ever the view appears it will select the first tab by default.


[self.tabBar setSelectedItem:self.tabBar.items[0]];

or

self.tabBar.selectedItem = self.tabBar.items[0];

The selectedItem property requires a TabBarItem and not an index. So provide the tabbaritem in index 0 for the first tab.

This is wrong then: tabbar.selectedItem=0;

You may select other tabs as well. Happy coding

0

精彩评论

暂无评论...
验证码 换一张
取 消