1

all images I put in Images.xcassets, so I can use images by [UIImage imageNamed:]. but when I open App, the tabBar not show all icons before I switch the tabItem or wait a moment.

- (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;
}

When I open my App, tabBar look like this: When I open my App, tabBar look like this This is my Images.xcassets: This is my Images.xcassets

NikSun
  • 45
  • 4

1 Answers1

0

That's because you use block statement. If you want to make UI changes, you have to do it through main thread.

try this code.

[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]];
            }
        });
        }];