简体   繁体   中英

UITabBarController selected item

I have UITabBarController and push on two views. I want the first view to be the default and the button to be in the "selected" state when it loads.

My problem is the tabBarItem does not have the selected background when it loads. I have to tap on it to show the selected. I'm also using custom images for the tab bar items as well and have them being configured in each view that gets added to the tabBarController.

I'm using this to set the selected item:

_tabBarController.selectedViewController = [_tabBarController.viewControllers objectAtIndex:0];

The UITabBarItem gets created in each view that I add to the tabBarController:

-(UITabBarItem *)tabBarItem
{
    return [[UITabBarItem alloc] initWithTitle:@"" image:[UIImage imageNamed:@"tab_select_indicator_map.png"]  tag:0];
}

How can I also have the tabBarItem show the selected state?

Use following code of display selected tabBarItem when your tabBar is loaded.

[_tabBarController setSelectedIndex:0]; // your can set index as per your requirement.

Eited:

Make sure that you created your tabBarItem such like

UITabBarItem* myTestingItem = [[UITabBarItem alloc] init];
[myTestingItem  setFinishedSelectedImage: [UIImage imageNamed: @"btnTabItem-SelectedImage.png"]
              withFinishedUnselectedImage: [UIImage imageNamed: @"btnTabItem.png"]];

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