UITabBarItem不显示所有图像或显示延迟图像

时间:2015-02-16 04:52:28

标签: ios objective-c uitabbaritem

我放在Images.xcassets中的所有图像,所以我可以通过[UIImage imageNamed:]使用图像。但是当我打开App时,tabBar在切换tabItem之前没有显示所有图标或者等一下。

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    // Override point for customization after application launch.
    [[UITabBar appearance] setTintColor:[UIColor colorWithRed:228/255.0 green:118/255.0 blue:54/255.0 alpha:1]];

    NSArray *tabBarItems = ((UITabBarController *)self.window.rootViewController).tabBar.items;
    NSArray *tabBarItemsTitle = @[@"TabGift", @"TabOnly", @"TabMissfresh", @"TabMine", @"TabCart"];

    [tabBarItems enumerateObjectsWithOptions:NSEnumerationConcurrent
                                        usingBlock:^(UITabBarItem *item, NSUInteger itemIndex, BOOL *stop) {
                                            [item setTitle:NSLocalizedString(tabBarItemsTitle[itemIndex], tabBarItemsTitle[itemIndex])];
                                            UIImage *image = [UIImage imageNamed:tabBarItemsTitle[itemIndex]];
                                            [item setImage: image];
                                            if ([tabBarItemsTitle[itemIndex] isEqualToString:@"TabMissfresh"]) {
                                                [item setSelectedImage:[[UIImage imageNamed:@"TabMissfreshSelected"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal]];
                                            }
                                        }];
    return YES;
}

当我打开我的应用程序时,tabBar看起来像这样: When I open my App, tabBar look like this 这是我的Images.xcassets: This is my Images.xcassets

1 个答案:

答案 0 :(得分:0)

那是因为你使用了block语句。 如果要进行UI更改,则必须通过主线程进行。

试试这段代码。

[tabBarItems enumerateObjectsWithOptions:NSEnumerationConcurrent
        usingBlock:^(UITabBarItem *item, NSUInteger itemIndex, BOOL *stop) {
        dispatch_sync(dispatch_get_main_queue(), ^{
            [item setTitle:NSLocalizedString(tabBarItemsTitle[itemIndex], tabBarItemsTitle[itemIndex])];
            UIImage *image = [UIImage imageNamed:tabBarItemsTitle[itemIndex]];
            [item setImage: image];
            if ([tabBarItemsTitle[itemIndex] isEqualToString:@"TabMissfresh"]) {
                [item setSelectedImage:[[UIImage imageNamed:@"TabMissfreshSelected"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal]];
            }
        });
        }];