如何在没有标题的情况下为UITabBarItem设置图像?

时间:2014-01-10 12:45:10

标签: ios iphone uitabbarcontroller uitabbar

我有一个应用程序,我正在以这样的程序方式添加UITabBarItems的图像..

    UIImage *selectedImage0 = [UIImage imageNamed:@"home_hvr.png"];
    UIImage *unselectedImage0 = [UIImage imageNamed:@"home.png"];

    UIImage *selectedImage1 = [UIImage imageNamed:@"star_hvr.png"];
    UIImage *unselectedImage1 = [UIImage imageNamed:@"star.png"];



    UITabBar *tabBar = self.tabbarcontroller.tabBar;
    self.tabbarcontroller.tabBar.barStyle = UIBarStyleBlack;
    self.tabbarcontroller.tabBar.translucent = NO;
    UITabBarItem *item0 = [tabBar.items objectAtIndex:0];
    UITabBarItem *item1 = [tabBar.items objectAtIndex:1];

    [item0 setSelectedImage:[selectedImage0 imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal]];
    [item0 setImage:[unselectedImage0 imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal]];

    [item1 setSelectedImage:[selectedImage1 imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal]];
    [item1 setImage:[unselectedImage1 imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal]];

由于tabbaritems没有任何标题,它会在标签栏底部带一个空白区域拉动标签栏上方的图像,我不需要标题,但需要将我的图像完全放在标签栏中。 任何人都可以指导我吗?

enter image description here

2 个答案:

答案 0 :(得分:1)

UITabBarItem.imageInsets = UIEdgeInsetsMake(6, 0, -6, 0);

完成了这个技巧对我有用。谢谢

答案 1 :(得分:0)

尝试以下代码:

[[self.tabBarController.tabBar.items objectAtIndex:0] setFinishedSelectedImage:[UIImage imageNamed:@"offer_btn_active.png"] withFinishedUnselectedImage:[UIImage imageNamed:@"offer_btn.png"]];

 [[self.tabBarController.tabBar.items objectAtIndex:1] setFinishedSelectedImage:[UIImage imageNamed:@"merchant_btn_active.png"] withFinishedUnselectedImage:[UIImage imageNamed:@"merchant_btn.png"]];