6

我通过设置每个项目的图像创建了一个简单的自定义标签栏,如下所示:

UITabBar *tabBar = self.tabBarController.tabBar;
UITabBarItem *item0 = [tabBar.items objectAtIndex:0];
UITabBarItem *item1 = [tabBar.items objectAtIndex:1];
UITabBarItem *item2 = [tabBar.items objectAtIndex:2];

[item0 setFinishedSelectedImage:[UIImage imageNamed:@"activity_active.png"] withFinishedUnselectedImage:[UIImage imageNamed:@"activity.png"]];
[item1 setFinishedSelectedImage:[UIImage imageNamed:@"agenda_active.png"] withFinishedUnselectedImage:[UIImage imageNamed:@"agenda.png"]];
[item2 setFinishedSelectedImage:[UIImage imageNamed:@"settings_active.png"] withFinishedUnselectedImage:[UIImage imageNamed:@"settings.png"]];

虽然这很好用,但我注意到我的标签栏下方有一个黑色空白区域

在此处输入图像描述

我的图像高度为 44 像素,但我认为我必须以某种方式更改标签栏的高度。

4

3 回答 3

2

tabBar 本身是 49px,它在图像后面(可能在 中)呈现为黑色[UITabBar layoutSubviews]。然后您的图像将呈现在顶部。偏移的原因是因为你提供的图片太大,UITabBar 需要 30x30px 的图标,而不是整个 UITabBarItem 的图片。

这里有几件事可以尝试:

  1. 只提供一个 30x30px 的图标,而不是整个标签按钮
  2. 在选项卡项目上设置图像后,试试这个: [item setImageInsets:UIEdgeInsetsMake(6, 0, -6, 0)]; // play with insets until it renders correctly
  3. 子类 UITabBar 并覆盖layoutSubviews到 first call super,然后根据需要重新定位您的图像。不推荐,它可能会在未来的 iOS 版本中中断。
于 2012-11-22T05:27:05.110 回答
0

利用 -

tabBar.frame=CGRectMake(x,y,w,h);

通过这种方式,您可以设置 xCord、yCord、宽度和高度。

于 2012-05-21T05:58:15.273 回答
0

检查这个:

[self.tabBar setFrame:CGRectMake(self.tabBar.frame.origin.x, self.tabBar.frame.origin.y - 30, self.tabBar.frame.size.width, self.tabBar.frame.size.height + 30)];
于 2014-03-17T10:55:49.237 回答