LoginSignup
14
14

More than 5 years have passed since last update.

タブバーの背景画像を設定する方法

Last updated at Posted at 2012-05-13
AppDelegate.h

// 以下のように、UITabBarControllerDelegateを追加します
@interface AppDelegate : UIResponder <UIApplicationDelegate, UITabBarControllerDelegate>

すると、以下のデリゲートメソッドが使えるようになります。

AppDelegate.m

// タブが5つあるとき
- (void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController
{
    [[self.tabBarController.tabBar.subviews lastObject] removeFromSuperview];
    // ↑これやらないとどんどん増えちゃいます。
    // 最初のTabBar背景画像は別のメソッドでaddするので、この位置でremoveします。

    NSString *tabBarImageName = @"";
    switch (tabBarController.selectedIndex) {
        case 0:
            tabBarImageName = @"tabBar-1.png";
            break;
        case 1:
            tabBarImageName = @"tabBar-2.png";
            break;
        case 2:
            tabBarImageName = @"tabBar-3.png";
            break;
        case 3:
            tabBarImageName = @"tabBar-4.png";
            break;
        case 4:
            tabBarImageName = @"tabBar-5.png";
            break;

        default:
            break;
    }
    UIImageView *tabBg = [[[UIImageView alloc] initWithImage:[UIImage imageNamed:tabBarImageName]] autorelease];
    [self.tabBarController.tabBar addSubview:tabBg];
}

ちなみにこの方法は、
タブ切替時にタブの背景すべてを変更するので、タブが5つあるなら、5つのタブ背景画像が必要です。

14
14
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
14
14