2
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

UITabBarのアイコンを選択時・未選択時で切り替える

Posted at

概要

UITabBarControllerを使う場合、アイコン画像を1種類セットしておけば、システムが自動的に未選択・選択時の色を変えてくれて便利です。
しかし、選択時にアイコンの白抜きを埋めたり、画像自体を切り替えたい場合、素直に実装できなかったので、今回行ったことを記します。

実装方法

他の方法もあるかもしれませんが、今回はAppDelegate内で実装しました。
アプリ起動時に画像をセットするような処理になっています。

AppDelegate.m
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    
    // TabBarController取得
    UITabBarController *tabBarController = (UITabBarController *) self.window.rootViewController;
    
    UITabBarItem *firstTabBar = [tabBarController.tabBar.items objectAtIndex:0];
    // タブ未選択時の画像をセット
    firstTabBar.image = [[UIImage imageNamed:@"tab_first_unselected"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    // タブ選択時の画像をセット
    firstTabBar.selectedImage = [[UIImage imageNamed:@"tab_first_selected"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];

    UITabBarItem *secondTabBar = [tabBarController.tabBar.items objectAtIndex:1];
    secondTabBar.image = [[UIImage imageNamed:@"tab_second_unselected"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    secondTabBar.selectedImage = [UIImage imageNamed:@"tab_second_selected"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];

    return YES;
}

こんな感じ

1439523391.jpg

2
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?