【问题标题】:iOS 5: UITabBarItem setFinishedSelectedImage:withFinishedUnselectedImage: not working / ignorediOS 5:UITabBarItem setFinishedSelectedImage:withFinishedUnselectedImage:不工作/被忽略
【发布时间】:2012-01-22 18:38:40
【问题描述】:

根据苹果docs

我正在尝试在 UITabBarItem 上设置自定义完成的选定和未选定图像,如下所示:


...
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];
...

基本上发生的事情是 TabBar 加载得很好,它只是完全忽略了标签栏项目的自定义。

我的目标是 iOS5+

图像是 30x30 透明 PNG,并且存在于项目中。无法弄清楚我在这里俯瞰什么,但一定是什么!

这是在 tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath 方法中调用的,也就是 Ray Wenderlich 的 tutorial

有人有什么想法吗?

谢谢!

【问题讨论】:

  • 注意:setFinishedSelectedImage:withFinishedUnselectedImage: 需要 iOS 5。

标签: iphone ios ios5 uitabbar uitabbaritem


【解决方案1】:

如果您试图在 UITabBar 上显示实际图像,请使用以下代码。

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

如果您想以原始状态显示所选图像,请使用以下内容

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

这两个是替代

setFinishedSelectedImage:  withFinishedUnselectedImage:

【讨论】:

    【解决方案2】:

    标签栏项使用方法初始化:initWithTabBarSystemItem:tag:。但是,正如文档所说:

    此方法返回系统提供的标签栏项目。返回项目的标题和图像属性以后无法更改。

    你应该用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"]];
    

    【讨论】:

    • 糟糕,我知道我错过了一些简单的东西 - 非常感谢!
    • 此方法已弃用。
    猜你喜欢
    • 1970-01-01
    • 2023-03-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多