如何使用可调整大小的图像与CapInsets将选项卡栏中的图像设置为全宽



我想实现的是让中间标签图像在不同的分辨率(i5、i6、i6+...)上拉伸,有 5 个标签,所以中间一个总是屏幕宽度的 1/5。

此代码未在中间选项卡的整个宽度上拉伸:

UITabBarItem *timelineTabBarItem = [[UITabBarItem alloc] initWithTitle:@"Middle"
                                                                 image:[[[UIImage imageNamed:@"tab3-on"] resizableImageWithCapInsets:UIEdgeInsetsMake(0, 31, 0, 32)] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal]
                                                         selectedImage:[[[UIImage imageNamed:@"tab3-off"] resizableImageWithCapInsets:UIEdgeInsetsMake(0, 31, 0, 32)] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal]];
timelineViewController.tabBarItem = timelineTabBarItem;

在 i6+ 上,我在选项卡中心看到图像。

我缺少什么/如何正确填写选项卡图像?

更新:

这是预览图像: https://i.stack.imgur.com/H1xUD.png

绿色是原始图像,应覆盖深蓝色区域。

试试这个,

UIImageView imgBgView =  [[UIImageView alloc] initWithFrame:CGRectMake(itemWidth*itemIndex,0,itemWidth,self.tabBarViewController.tabBar.frame.size.height)];
imgBgView.image = [[UIImage imageNamed:@"tab_icon_checkin_inactive.png"] stretchableImageWithLeftCapWidth:0 topCapHeight:0];
imgBgView.contentMode = UIViewContentModeScaleAspectFit;
[self.tabBar.tabBar insertSubview:imgBgView atIndex:0];

这里的itemWidth是一个选项卡的宽度,以使其使用,

float itemWidth = [[UIScreen mainScreen] bounds].size.width / (float)(self.tabBarViewController.tabBar.items.count);

它将在选项卡栏中间的选项卡中设置图像。

希望对您有所帮助。

最新更新