我必须在TabBar的部分之间添加分隔符,如下图所示:
我试图设置背景iamge为tabbar使用这个图像:
但是当我旋转设备时我有问题.
我使用的代码:
+ (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项,但没有结果.
请问,请帮帮我吗?
谢谢 !
解决方法
您可以通过编程方式使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,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,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中调用此方法.