微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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中调用方法.

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐