简体   繁体   中英

How to set first TabBar selected programmatically on iPhone

I have UITabBar in view which have 5 tabs. I am using didSelectItem delegate to open different view ie 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 it selected?

Thanks.

此代码将起作用[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
[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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM