代码之家  ›  专栏  ›  技术社区  ›  Frank

有没有一种方法可以为uitabaritem使用自定义选定的图像?

  •  8
  • Frank  · 技术社区  · 15 年前

    当用户在选项卡栏上选择一个项目时,我希望有一个自定义选定的图像,默认情况下,它选择蓝色,但希望有一个绿色代替。有什么想法吗?

    alt text http://www.freeimagehosting.net/uploads/11a2137011.png

    9 回复  |  直到 12 年前
        1
  •  11
  •   Frank    14 年前

    刚刚找到我的解决方案。基本上,我将uitabitem子类化,并在导航控制器中设置它:

    -(void) viewDidAppear:(BOOL)animated {
        [super viewDidAppear:animated];
    
        CustomTabBarItem *tabItem = [[CustomTabBarItem alloc] initWithTitle:@"Events" image:[UIImage imageNamed:@"tabIcon.png"] tag:0];
        tabItem.customHighlightedImage=[UIImage imageNamed:@"tabIconSelected.png"];
        self.tabBarItem = tabItem;
        [tabItem release];
        tabItem=nil;            
    }
    

    下面是CustomTabBarItem类的外观:

    @interface CustomTabBarItem : UITabBarItem
    {
        UIImage  *customHighlightedImage;
    }
    
    @property (nonatomic, retain) UIImage *customHighlightedImage;
    
    @end
    

    实施:

    #import "CustomTabBarItem.h
    
    @implementation CustomTabBarItem
    
    @synthesize customHighlightedImage;
    
    - (void)dealloc {
        [customHighlightedImage release];
        customHighlightedImage=nil;
        [super dealloc];
    }
    
    -(UIImage *)selectedImage {
        return self.customHighlightedImage;
    }
    
    @end
    
        2
  •  6
  •   Muhammad Rizwan    12 年前

    在iOS 6中,我将所选选项卡项图像更改为-

    在Tabbar控制器委托方法中

    - (void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController
    
    {
        if([tabBarController selectedIndex] == 0)
        {
            [viewController.tabBarItem setFinishedSelectedImage:[UIImage imageNamed:@"selected.png"]withFinishedUnselectedImage:[UIImage imageNamed:@"unselect.png"]];
        }    
    }
    

    通过这个你可以改变你的形象。

    也可以直接在视图控制器init(或viewwillappear)方法中使用,例如

            [viewController.tabBarItem setFinishedSelectedImage:[UIImage imageNamed:@"selected.png"]withFinishedUnselectedImage:[UIImage imageNamed:@"unselect.png"]];
    

    我希望这对你有帮助。

        3
  •  2
  •   cutsoy    15 年前

    只需在调用UITabArcController委托方法时添加一些自定义视图(使用insertSubView:atindex:)。

    例子:

    – (void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController {
        [tabBarController.tabBar insertSubview:someView atIndex:someIndex];
    }
    

    你可以尝试改变 someIndex 直到你得到你想要的结果。

        4
  •  2
  •   Justin    15 年前

    这在SDK中不是官方支持的。您可能能够在运行时探测和调整选项卡的视图,但您有被苹果拒绝的风险。

    编辑:为了完整性,我应该提到您的另一个选择是滚动您自己的uitabbar。

        5
  •  1
  •   JosephH    13 年前

    对于IO5及以上版本,您只需执行以下操作:

    rootTabBarController.tabBar.selectedImageTintColor = [UIColor greenColor];
    
        6
  •  1
  •   Popeye swathy krishnan    12 年前

    我相信你现在可以用:

    [[[[self tabBar] items] objectAtIndex:0] setFinishedSelectedImage:nil withFinishedUnselectedImage:nil];
    
        7
  •  1
  •   philm5    12 年前

    使用故事板时,只需选择TabbarController的Tabbar,然后在Identity Inspector中更改图像色调。这也适用于XIB。

    Look here for an image describing the todos

        8
  •  0
  •   Ashoor    12 年前

    在appdelegate.m中

    - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions (NSDictionary *)launchOptions {
    
    [[UITabBar appearance] setSelectedImageTintColor:[UIColor redColor]]; 
    
    return YES;
    
    }
    

    这将给你一个红色,改变颜色与你希望的白色,蓝色等。

        9
  •  0
  •   Dustin    12 年前

    在我的UitababarController's viewDidLoad: 根据Rizzu的回答:

    for (int i = 0; i < [self.viewControllers count]; i++)
    {
        UIViewController* viewController = [self.viewControllers objectAtIndex:i];
    
        if(i == 0)
        {
            [viewController.tabBarItem setFinishedSelectedImage:[UIImage imageNamed:@"btn_list_all_hover.png"]withFinishedUnselectedImage:[UIImage imageNamed:@"btn_list_all.png"]];
        }
        else if(i == 1)
        {
            [viewController.tabBarItem setFinishedSelectedImage:[UIImage imageNamed:@"btn_settings_hover.png"]withFinishedUnselectedImage:[UIImage imageNamed:@"btn_settings.png"]];
        }
    }