如何在UIITabBar中将完整图像设置为按钮?

时间:2014-06-26 23:19:28

标签: ios iphone uitabbarcontroller uitabbar

我想知道如何完全将UITabBar按钮完全替换为图像(即图像下方没有任何文本,类似于Instagram或Polar中的样式)。

我尝试使用此guide中建议的[tabBarItem1 setFinishedSelectedImage:[UIImage imageNamed:@"option1Selected.png"] withFinishedUnselectedImage:[UIImage imageNamed:@"option1.png"]];,但它仍然包含底部的文字。感谢帮助 - 谢谢!

enter image description here enter image description here

1 个答案:

答案 0 :(得分:1)

您需要设置edge insets并在image rendering options及更高版本中使用不同的iOS 7。我使用了一个简单的OS_version check并按如下方式实现:

OS_version检查定义一个常量:

#define DEVICE_SYSTEM_VERSION [[[UIDevice currentDevice] systemVersion] floatValue]

然后在基本viewController中使用以下代码:

UIImage *yourSelectedImage = [UIImage imageNamed:@"option1Selected.png"];
UIImage *yourUnSelectedImage = [UIImage imageNamed:@"option1.png"];

viewController.tabBarItem = [[UITabBarItem alloc] init];

if (DEVICE_SYSTEM_VERSION >= 7) 
{
    [yourViewController.tabBarItem setImage:[yourUnSelectedImage imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal]];
    [yourViewController.tabBarItem setSelectedImage:[yourSelectedImage imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal]];
} 
else 
{
    [yourViewController.tabBarItem setImage:yourUnSelectedImage];
    [yourViewController.tabBarItem setFinishedSelectedImage:yourSelectedImage withFinishedUnselectedImage:image];
}


// set the title to be an empty string
yourViewController.tabBarItem.title = @"";

// set edge insets so that the image occupies the entire area
if (DEVICE_SYSTEM_VERSION <= 7) 
{
    yourViewController.tabBarItem.imageInsets = UIEdgeInsetsMake(8, 0, -8, 0);
}
else
{
    yourViewController.tabBarItem.imageInsets = UIEdgeInsetsMake(0, 0, 0, 0);
}

这对我有用。希望这可以帮助! :)