ios – 在TabBar中的段之间添加分隔符

我必须在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中调用方法.

相关文章

UITabBarController 是 iOS 中用于管理和显示选项卡界面的一...
UITableView的重用机制避免了频繁创建和销毁单元格的开销,使...
Objective-C中,类的实例变量(instance variables)和属性(...
从内存管理的角度来看,block可以作为方法的传入参数是因为b...
WKWebView 是 iOS 开发中用于显示网页内容的组件,它是在 iO...
OC中常用的多线程编程技术: 1. NSThread NSThread是Objecti...