UITabBar未在ios 7中显示所选项目图像

时间:2013-09-19 12:46:12

标签: objective-c uitabbar ios7

图标在ios 6中显示正常但在ios 7中没有显示。我在viewController viewDidLoad方法中设置了所选状态。当用户选择标签栏项时,图像消失。 这是我的代码:

UITabBar *tabBar = self.tabBarController.tabBar;
if ([UITabBar instancesRespondToSelector:@selector(setSelectedImageTintColor:)]) {
    [self.tabBarController.tabBar setSelectedImageTintColor:[UIColor whiteColor]];
}
UITabBarItem *item0 = [tabBar.items objectAtIndex:0];
UITabBarItem *item1 = [tabBar.items objectAtIndex:1];
UITabBarItem *item2 = [tabBar.items objectAtIndex:2];
UITabBarItem *item3 = [tabBar.items objectAtIndex:3];
[item0 setTitle:@"Home"];
[item1 setTitle:@"Calendar"];
[item2 setTitle:@"News"];
[item3 setTitle:@"My Events"];
[item0 setFinishedSelectedImage:[UIImage imageNamed:@"homeIconSelected.png"] withFinishedUnselectedImage:[UIImage imageNamed:@"home2.png"]];
[item1 setFinishedSelectedImage:[UIImage imageNamed:@"Calendar"] withFinishedUnselectedImage:[UIImage imageNamed:@"CalendarIconSelected"]];
[item2 setFinishedSelectedImage:[UIImage imageNamed:@"NewsIconSelected"] withFinishedUnselectedImage:[UIImage imageNamed:@"News"]];
[item3 setFinishedSelectedImage:[UIImage imageNamed:@"EventsIconSelected"] withFinishedUnselectedImage:[UIImage imageNamed:@"Events"]];
[item1 imageInsets];
[item2 imageInsets];
[item3 imageInsets];

19 个答案:

答案 0 :(得分:95)

您需要使用tabBarItem initWithTitle:image:selectedImage

[[UITabBarItem alloc] initWithTitle:@"title" image:image selectedImage:imageSel];

结合更改UIImage渲染模式:

imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal

或(要应用父视图模板色调掩码,除非您选择退出上述渲染模式,否则此选项是标签栏项目的默认选项)

imageWithRenderingMode:UIImageRenderingModeAlwaysTemplate

这是一个标签栏项目的代码示例: -

UIImage *musicImage = [UIImage imageNamed:@"music.png"];
UIImage *musicImageSel = [UIImage imageNamed:@"musicSel.png"];

musicImage = [musicImage imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
musicImageSel = [musicImageSel imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];

self.musicViewController.tabBarItem = [[UITabBarItem alloc] initWithTitle:@"Music" image:musicImage selectedImage:musicImageSel];

希望这有帮助

答案 1 :(得分:53)

将条形图像的渲染模式设置为原始可以解决此问题。 这可以通过使用.xcassets中的图像来完成,因此您不必编写大量代码。

首先,将条形图像放置并放置到Assets.xcassets。

第二步,选择条形图像,然后将[渲染为]更改为[原始图像]

enter image description here

ps:我通常会通过故事板设置TabBarController的标签栏项目,以避免编写大量代码。

enter image description here

答案 2 :(得分:45)

中添加以下代码行
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
UITabBarController *tabBarController = (UITabBarController *)self.window.rootViewController;
UITabBar *tabBar = tabBarController.tabBar;
UITabBarItem *tabBarItem1 = [tabBar.items objectAtIndex:0];
UITabBarItem *tabBarItem2 = [tabBar.items objectAtIndex:1];
UITabBarItem *tabBarItem3 = [tabBar.items objectAtIndex:2];
UITabBarItem *tabBarItem4 = [tabBar.items objectAtIndex:3];

tabBarItem1.selectedImage = [[UIImage imageNamed:@"selectimg"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal ];
tabBarItem1.image = [[UIImage imageNamed:@"deselectimg"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal ];
tabBarItem1.title = @"xxxx";

tabBarItem2.selectedImage = [[UIImage imageNamed:@"selectimg"]imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal ];
tabBarItem2.image = [[UIImage imageNamed:@"deselectimg"]imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal ];
tabBarItem2.title = @"xxxx";

tabBarItem3.selectedImage = [[UIImage imageNamed:@"selectimg"]imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal ];
tabBarItem3.image = [[UIImage imageNamed:@"deselectimg"]imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal ];
tabBarItem3.title = @"xxxx";

tabBarItem4.selectedImage = [[UIImage imageNamed:@"selectimg"]imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal ];
tabBarItem4.image = [[UIImage imageNamed:@"deselectimg"]imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal ];
tabBarItem4.title = @"xxxx";

return YES;
}

这对我有用......并希望最好......

答案 3 :(得分:16)

没有答案有助于解决此问题。主要原因是我的TabBarController不是我的RootViewController

我用于Storyboards的解决方案,我只是点击了我的UITabButton,我为selectedImage添加了一个运行时属性:

对于与UITabController相关联的每个不同视图。

答案 4 :(得分:9)

花了几个小时试图让我的自定义标签栏适用于iOS 6& 7,这对我有用......

UITabBarController *tabBarController = (UITabBarController *)[[self window] rootViewController];

UITabBar *tabBar = tabBarController.tabBar;
UITabBarItem *tabBarItem1 = [tabBar.items objectAtIndex:0];
UITabBarItem *tabBarItem2 = [tabBar.items objectAtIndex:1];
UITabBarItem *tabBarItem3 = [tabBar.items objectAtIndex:2];
UITabBarItem *tabBarItem4 = [tabBar.items objectAtIndex:3];

tabBarItem1.title = @"Home";
tabBarItem2.title = @"Map";
tabBarItem3.title = @"Weather";
tabBarItem4.title = @"Info";

if ([[[UIDevice currentDevice] systemVersion] floatValue] < 7) {
    [tabBarItem1 setFinishedSelectedImage:[UIImage imageNamed:@"cyexplore_home_white.png"] withFinishedUnselectedImage:[UIImage imageNamed:@"cyexplore_home_black.png"]];
    [tabBarItem2 setFinishedSelectedImage:[UIImage imageNamed:@"cyexplore_cloud_white.png"] withFinishedUnselectedImage:[UIImage imageNamed:@"cyexplore_cloud_black.png"]];
    [tabBarItem3 setFinishedSelectedImage:[UIImage imageNamed:@"cyexplore_map_white.png"] withFinishedUnselectedImage:[UIImage imageNamed:@"cyexplore_map_black.png"]];
    [tabBarItem4 setFinishedSelectedImage:[UIImage imageNamed:@"cyexplore_info_white.png"] withFinishedUnselectedImage:[UIImage imageNamed:@"cyexplore_info_black.png"]];
} else {
    tabBarItem1.selectedImage = [[UIImage imageNamed:@"cyexplore_home_white"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal ];
    tabBarItem1.image = [[UIImage imageNamed:@"cyexplore_home_black"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal ];

    tabBarItem2.selectedImage = [[UIImage imageNamed:@"cyexplore_cloud_white"]imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal ];
    tabBarItem2.image = [[UIImage imageNamed:@"cyexplore_cloud_black"]imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal ];

    tabBarItem3.selectedImage = [[UIImage imageNamed:@"cyexplore_map_white"]imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal ];
    tabBarItem3.image = [[UIImage imageNamed:@"cyexplore_map_black"]imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal ];

    tabBarItem4.selectedImage = [[UIImage imageNamed:@"cyexplore_info_white"]imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal ];
    tabBarItem4.image = [[UIImage imageNamed:@"cyexplore_info_black"]imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal ];
}

UIImage* tabBarBackground = [UIImage imageNamed:@"tabbar.png"];
[[UITabBar appearance] setBackgroundImage:tabBarBackground];
[[UITabBar appearance] setSelectionIndicatorImage:[UIImage imageNamed:@"tabbar_selected.png"]];
[[UITabBarItem appearance] setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:[UIColor blackColor], UITextAttributeTextColor, nil] forState:UIControlStateNormal];
[[UITabBarItem appearance] setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:[UIColor whiteColor], UITextAttributeTextColor, nil] forState:UIControlStateSelected];

答案 5 :(得分:8)

如果您正在使用故事板,则必须在导航控制器中输入标识符:“custom”。

然后:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    // Assign tab bar item with titles
    UITabBarController *tabBarController = (UITabBarController *)self.window.rootViewController;
    UITabBar *tabBar = tabBarController.tabBar;
    UITabBarItem *tabBarItem1 = [tabBar.items objectAtIndex:0];
    UITabBarItem *tabBarItem2 = [tabBar.items objectAtIndex:1];
    UITabBarItem *tabBarItem3 = [tabBar.items objectAtIndex:2];

    (void)[tabBarItem1 initWithTitle:nil image:[UIImage imageNamed:@"home.png"] selectedImage:[UIImage imageNamed:@"home_selected.png"]];
    (void)[tabBarItem2 initWithTitle:nil image:[UIImage imageNamed:@"home.png"] selectedImage:[UIImage imageNamed:@"home_selected.png"]];
    (void)[tabBarItem3 initWithTitle:nil image:[UIImage imageNamed:@"home.png"] selectedImage:[UIImage imageNamed:@"home_selected.png"]];

    // Change the tab bar background
    UIImage* tabBarBackground = [UIImage imageNamed:@"tabbar.png"];
    [[UITabBar appearance] setBackgroundImage:tabBarBackground];



    return YES;
}

这适合我。

答案 6 :(得分:3)

这些答案都不适用于我 - 我使用的是MonoTouch,但如果您设置TintColor本身的UITabBar属性,则会突出显示所选图像的颜色。在对象中它可能是setTintColor函数。

答案 7 :(得分:3)

我遇到了与Xcode 6.0.1(6A317)相同的问题,似乎是Interface builder的一个错误。 但是,我已经设法通过在界面构建器中将selected image留空来然后在我的视图控制器中的每个viewDidLoad处设置为空来解决它。我插入了:

[self.navigationController.tabBarItem setSelectedImage:[UIImage imageNamed:@"imagename-selected"]];

它现在运作良好,显示我的selectedImage和全局色调模板。

答案 8 :(得分:2)

在您的第一个视图控制器的.h文件中,我添加了以下内容:     @property(弱,非原子)IBOutlet UITabBarItem * mapViewTabBarItem;     @property(弱,非原子)IBOutlet UITabBarItem * profileViewTabBarItem;     @property(弱,非原子)IBOutlet UITabBarItem * notificationViewTabBarItem;

(请注意,mapViewTabBarItem是通过ctrl将实际标签栏项拖动到.h文件顶部的属性声明列表中链接的)

接下来,在viewDidLoad中的同一视图控制器的.m文件中,添加以下内容:

self.tabBarItem = [self.tabBarController.tabBar.items objectAtIndex:0];
_mapViewTabBarItem.selectedImage = [[UIImage imageNamed:@"@2x-map-icon-selected.png"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal ];
self.tabBarItem.image = [[UIImage imageNamed:@"@2x-map-icon.png"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal ];

_profileViewTabBarItem = [self.tabBarController.tabBar.items objectAtIndex:1];
_profileViewTabBarItem.selectedImage = [[UIImage imageNamed:@"@2x-profile-icon-selected.png"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal ];
_profileViewTabBarItem.image = [[UIImage imageNamed:@"@2x-profile-icon.png"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal ];

_notificationViewTabBarItem = [self.tabBarController.tabBar.items objectAtIndex:2];
_notificationViewTabBarItem.selectedImage = [[UIImage imageNamed:@"@2x-notifications-icon-selected.png"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal ];
_notificationViewTabBarItem.image = [[UIImage imageNamed:@"@2x-notifications-icon.png"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal ];

答案 9 :(得分:2)

我有类似的问题。我在故事板中创建了标签栏,并通过界面构建​​器菜单添加了所有图像,代码中没有。

我的修复实际上很简单:在IB中的属性检查器窗口下,&#34;选择图像&#34;的标签栏项目字段应为空白,并且&#34;图像&#34;的条形图项字段应该填写你想要的图像。

我正在运行Xcode 6.0.1并使用iOS 8.0.2设备进行测试。

答案 10 :(得分:2)

你应该写信给函数:

UIImage* tab_image = [UIImage imageNamed:@"tab_image.png"];
UIImage* tab_image_selected = [UIImage imageNamed:@"tab_image_selected.png"];

tab_image = [tab_image imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
tab_image_selected = [tab_image_selected imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];

self.tabBarItem.image = tab_image;
self.tabBarItem.selectedImage = tab_image_selected;

我希望这会有所帮助

答案 11 :(得分:2)

根据132206的回答,我为AppDelegate制作了这个方法,从application:didFinishLaunchingWithOptions:调用:

[self configureSelectedImages];

显然,您需要对选项卡图像采用严格的命名约定,但也可以在不进行编辑的情况下重复使用。要说明显而易见的事项 - 将选定的标签栏图像命名为TabbarXXXSelected,其中XXX等于标签栏项目的标题。

- (void)configureSelectedImages
{
    UITabBarController *tabBarController = (UITabBarController *)self.window.rootViewController;
    UITabBar *tabBar = tabBarController.tabBar;

    for (UITabBarItem *tabBarItem in [tabBar items]) {
        NSString *selectedImage = [NSString stringWithFormat:@"Tabbar%@Selected", [tabBarItem title]];
        (void)[tabBarItem initWithTitle:[tabBarItem title] image:[tabBarItem image] selectedImage:[UIImage imageNamed:selectedImage]];
    }
}

答案 12 :(得分:1)

试试这个:

UITabBarItem *item1 = [[UITabBarItem alloc] initWithTitle:@"" image:[UIImage imageNamed:@"Icon-Small-50.png"] tag:100];
UITabBarItem *item2 = [[UITabBarItem alloc] initWithTitle:@"" image:[UIImage imageNamed:@"image-50.png"] tag:200];
UITabBarItem *item3 = [[UITabBarItem alloc] initWithTitle:@"" image:[UIImage imageNamed:@"Icon-clip-50.png"] tag:300];
UITabBarItem *item4 = [[UITabBarItem alloc] initWithTitle:@"" image:[UIImage imageNamed:@"Icon-color-50.png"] tag:400];
UITabBarItem *item5 = [[UITabBarItem alloc] initWithTitle:@"" image:[UIImage imageNamed:@"Icon-lock-50.png"] tag:500];

[item1 setSelectedImage:[[UIImage imageNamed:@"Icon-Small-50.png"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal]];
[item1 setImageInsets:UIEdgeInsetsMake(0, 0, 0, 0)];

[item2 setSelectedImage:[[UIImage imageNamed:@"image-50.png"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal]];
[item2 setImageInsets:UIEdgeInsetsMake(0, 0, 0, 0)];

[item3 setSelectedImage:[[UIImage imageNamed:@"Icon-clip-50.png"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal]];
[item3 setImageInsets:UIEdgeInsetsMake(0, 0, 0, 0)];

[item4 setSelectedImage:[[UIImage imageNamed:@"Icon-color-50.png"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal]];
[item4 setImageInsets:UIEdgeInsetsMake(0, 0, 0, 0)];

[item5 setSelectedImage:[[UIImage imageNamed:@"Icon-lock-50.png"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal]];
[item5 setImageInsets:UIEdgeInsetsMake(0, 0, 0, 0)];

item1.image = [[UIImage imageNamed:@"Icon-Small-50.png"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];

item2.image = [[UIImage imageNamed:@"image-50.png"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];

item3.image = [[UIImage imageNamed:@"Icon-clip-50.png"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
item4.image = [[UIImage imageNamed:@"Icon-color-50.png"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
item5.image = [[UIImage imageNamed:@"Icon-lock-50.png"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];

答案 13 :(得分:0)

使用以下代码修复iOS7中的图像问题:

[[UITabBarItem alloc] initWithTitle:@"title" image:[[UIImage imageNamed:@"YOUR_IMAGE.png"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] selectedImage:[[UIImage imageNamed:@"YOUR_SEL_IMAGE.png"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal]];

答案 14 :(得分:0)

我有同样的问题。但是使用StoryBoards阻止我改变代码中的任何内容。在故事板中留下空白的图像为我取消了这个障碍。但是将initWithTitle放在选项卡viewcontroller的viewWillAppear方法中给了我奇怪的行为。首先获取所选图像需要额外点击,并且图像未显示非初始标签。

对我来说,修复此问题是在DidFinishLoadingWithOptions中添加以下代码到AppDelegate(类似于132206和Amitabha):

NSArray * vcs = [(UITabBarController*)self.window.rootViewController viewControllers];
UIViewController *tab0 = [[(UINavigationController*)[vcs objectAtIndex:0] viewControllers] objectAtIndex:0];
tab0.title = NSLocalizedString(@"Time", nil);
tab0.tabBarItem =  [[UITabBarItem alloc] initWithTitle:tab0.title image:[UIImage imageNamed:@"Recents.png"] selectedImage:[UIImage imageNamed:@"RecentsSolid.png"]];
UIViewController *tab1 = [[(UINavigationController*)[vcs objectAtIndex:1] viewControllers] objectAtIndex:0];
tab1.title = NSLocalizedString(@"Expense", nil);
tab1.tabBarItem = [[UITabBarItem alloc] initWithTitle:tab1.title image:[UIImage imageNamed:@"Euro.png"] selectedImage:[UIImage imageNamed:@"EuroSolid.png"]];

答案 15 :(得分:0)

UITabBar Items的类别简单易用。

只需创建类别并使用运行时属性并从类别中引用它,如下所示。 Add Runtime Attribute for selected TabBarItem Refer it from category and make changes

#import "UITabBarItem+CustomTabBar.h"

@implementation UITabBarItem (CustomTabBar)

-(void)setValue:(id)value forKey:(NSString *)key {
    if([key isEqualToString:@"tabtitle"]){
        if([value isEqualToString:@"contacts"]) {
            [self setSelectedImage:[[UIImage imageNamed:@"contacts-selected"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal]];
        } else if([value isEqualToString:@"chat"]) {
            [self setSelectedImage:[[UIImage imageNamed:@"chat-selected"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal]];
        } else if([value isEqualToString:@"groupchat"]) {
            [self setSelectedImage:[[UIImage imageNamed:@"groupchat-selected"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal]];
        } else if([value isEqualToString:@"settings"]) {
            [self setSelectedImage:[[UIImage imageNamed:@"settings-selected"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal]];
        }
    }
    [self setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:[UIFont fontWithName:@"Roboto-Regular" size:12.0f], NSFontAttributeName, [UIColor grayColor], NSForegroundColorAttributeName, nil] forState:UIControlStateNormal];
}

@end

答案 16 :(得分:0)

旧问题,但是也要在这里添加我的解决方案以及Xamarin.iOS / C#以及那些想要通过Interface builder设置图像的人。我通过Interface Builder设置了Selected ImageImage属性。然后在代码中我定义了一个InitTabs()方法,如下所示:

public void InitTabs(){
        HomeTab.SelectedImage = HomeTab.SelectedImage.ImageWithRenderingMode(UIImageRenderingMode.AlwaysOriginal);
        HomeTab.Image = HomeTab.Image.ImageWithRenderingMode(UIImageRenderingMode.AlwaysOriginal);
}

InitTabs()中呼叫ViewDidLoad,现在将为所选状态和未选择状态显示正确的图像。

答案 17 :(得分:0)

以下是Swift-Guys的Swift解决方案:)

class CustomTabBar: UITabBar {

    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)

        let btnNames = ["Title1", "Title2", "Title3", "Title4"]

        for (item, name) in zip(items!, btnNames) {
            item.image = UIImage(named: "bar\(name)Btn")?.imageWithRenderingMode(.AlwaysOriginal)
            item.selectedImage = UIImage(named: "bar\(name)SelectedBtn")?.imageWithRenderingMode(.AlwaysOriginal)
            item.title = name
            item.setTitleTextAttributes([NSForegroundColorAttributeName: UIColor.blackColor()], forState: .Normal)
            item.setTitleTextAttributes([NSForegroundColorAttributeName: UIColor.redColor()], forState: .Selected)
        }
    }

}

这里到底发生了什么:

  • 制作btn标题数组并考虑图像文件名以匹配它们
  • Make for循环选项卡栏项目,刚刚创建了btn titles array
  • 从数组
  • 设置barButtonItem的图像及其selectedImage
  • 从数组中设置标题文字
  • 为州.Normal和.Selected
  • 设置标题文字颜色

设置文本颜色部分非常重要,如果您不希望将项目的标题颜色保持为灰色.Normal和蓝色表示.Selected,默认情况下是这样。当您考虑标签栏项目的自定义图像时,这通常是实际的。

答案 18 :(得分:0)

在尝试了所有其他答案并在失败时挣扎之后,我找到了答案。其他答案似乎不适用于当前的快速版本。在Swift 2.3中,这对我有用。对于那些仍然遇到麻烦的人,试试这个:

tabBarItem.image = UIImage(named: "image_name")
searchVC.tabBarItem.selectedImage = UIImage(named:
     "image_name_when_selected")?.imageWithRenderingMode(.AlwaysOriginal)