iOS 5: UITabBarItem setFinishedSelectedImage:withFinishedUnselectedImage: not working / ignored

你。 提交于 2019-12-09 01:47:16

问题


As per Apple docs

I'm trying to set custom finished selected and unselected images on a UITabBarItem like so:


...
DetailViewController *vc1 = [[DetailViewController alloc] initWithNibName:@"DetailView" bundle:nil];
UITabBarItem *vc1i = [[UITabBarItem alloc] initWithTabBarSystemItem:UITabBarSystemItemTopRated tag:100];
[vc1i setFinishedSelectedImage:[UIImage imageNamed:@"tab_bar_item_selected.png"] withFinishedUnselectedImage:[UIImage imageNamed:@"tab_bar_item_normal.png"]];
[vc1 setTabBarItem:vc1i];
...

Basically what's happening is the TabBar loads up just fine, it just completely ignores the tab bar item customization.

I'm targeting iOS5+

The images are 30x30 transparent PNGs and exist in the project. Can't figure out what I'm overlooking here, but must be something!

This is being called in the tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath method, ala Ray Wenderlich's tutorial

Anyone have any ideas?

Thanks!


回答1:


The tab bar item is initialized with the method: initWithTabBarSystemItem:tag:. But, as the documentation says:

This method returns a system-supplied tab bar item. The title and image properties of the returned item cannot be changed later.

You should initialize the tab bar item with initWithTitle:image:tag:.

UITabBarItem *vc1i = [[UITabBarItem alloc] initWithTitle:@"Top Rated" image:nil tag:100];
[vc1i setFinishedSelectedImage:[UIImage imageNamed:@"tab_bar_item_selected.png"] withFinishedUnselectedImage:[UIImage imageNamed:@"tab_bar_item_normal.png"]];



回答2:


If your are trying to achieve displaying of the actual image at the UITabBar then use the following code.

[yourTabBarItem setImage:[[UIImage imageNamed:@"more.png"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal]];

and if you want to display image in original condition for the selected then use the following

[yourTabBarItem setSelectedImage:[[UIImage imageNamed:@"more.png"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal]];

these two are alternative to

setFinishedSelectedImage:  withFinishedUnselectedImage:


来源:https://stackoverflow.com/questions/8547669/ios-5-uitabbaritem-setfinishedselectedimagewithfinishedunselectedimage-not-wo

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!