在TabBar中的部分之间添加分隔符 [英] Add separator between section in TabBar
问题描述
我必须在TabBar中的部分之间添加分隔符,如下图所示:
I have to add separator between section in TabBar as in image below:
我尝试使用此图像设置tabbar的背景图片:
但我在旋转设备时遇到问题。
I tried to set the background image for tabbar using this image: but I have problems when I rotate the device.
我使用的代码:
+ (UITabBarController *)loadTabbar
{
UITabBarController *tabBarController = [UITabBarController new];
MenuVC *viewController0 = [MenuVC new];
FavVC *viewController1 = [FavVC new];
UploadVC *viewController2 = [UploadVC new];
RestoreVC *viewController3 = [RestoreVC new];
SettingsVC *viewController4 = [SettingsVC new];
tabBarController.viewControllers = @[viewController0, viewController1, iewController2, viewController3, viewController4];
[tabBarController.tabBar setBackgroundImage:[UIImage mageNamed:@"tabbar_color"]];
[self setRootController:tabBarController];
return tabBarController;
}
另外,我试图在图像的右侧添加一个分隔符用于abbar项目但没有结果。
你能帮我提一下建议吗?
Also, I tried to add a separator on the right side of image that I used for abbar item but without result. Can you, please, help me with any advice ?
谢谢!
推荐答案
您可以以编程方式为UITabBar创建背景:
You can make programmatically a background for UITabBar:
#define SEPARATOR_WIDTH 0.4f
#define SEPARATOR_COLOR [UIColor whiteColor]
- (void) setupTabBarSeparators {
CGFloat itemWidth = floor(self.tabBar.frame.size.width/self.tabBar.items.count);
UIView *bgView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.tabBar.frame.size.width, self.tabBar.frame.size.height)];
for (int i=0; i<self.tabBar.items.count - 1; i++) {
UIView *separator = [[UIView alloc] initWithFrame:CGRectMake(itemWidth * (i +1) - SEPARATOR_WIDTH/2, 0, SEPARATOR_WIDTH, self.tabBar.frame.size.height)];
[separator setBackgroundColor:SEPARATOR_COLOR];
[bgView addSubview:separator];
}
UIGraphicsBeginImageContext(bgView.bounds.size);
[bgView.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage *tabBarBackground = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
[[UITabBar appearance] setBackgroundImage:tabBarBackground];
}
你应该只扩展UITabBarController并制作一个自定义的UITabBarViewController。您应该在viewDidLoad和willRotateToInterfaceOrientation中调用此方法。
You should only extend UITabBarController and make a custom UITabBarViewController. You should call this method in viewDidLoad and in willRotateToInterfaceOrientation.
这篇关于在TabBar中的部分之间添加分隔符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!