ios - 在 TabBar 中的部分之间添加分隔符

标签 ios objective-c uitabbarcontroller uitabbar

我必须在 TabBar 的部分之间添加分隔符,如下图所示: enter image description here

我尝试使用此图像为标签栏设置背景图像: enter image description here 但是我在旋转设备时遇到了问题。

我使用的代码:

 + (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 项目的图像右侧添加一个分隔符,但没有结果。 你能帮我提点建议吗?

谢谢!

最佳答案

我刚刚转换了@bojanb89's answer到 Swift 3。这不会呈现背景图像,而只是在每个选项卡栏项目之间添加一个 View 。

func setupTabBarSeparators() {
    let itemWidth = floor(self.tabBar.frame.size.width / CGFloat(self.tabBar.items!.count))

    // this is the separator width.  0.5px matches the line at the top of the tab bar
    let separatorWidth: CGFloat = 0.5

    // iterate through the items in the Tab Bar, except the last one
    for i in 0...(self.tabBar.items!.count - 1) {
        // make a new separator at the end of each tab bar item
        let separator = UIView(frame: CGRect(x: itemWidth * CGFloat(i + 1) - CGFloat(separatorWidth / 2), y: 0, width: CGFloat(separatorWidth), height: self.tabBar.frame.size.height))

        // set the color to light gray (default line color for tab bar)
        separator.backgroundColor = UIColor.lightGray

        self.tabBar.addSubview(separator)
    }
}

关于ios - 在 TabBar 中的部分之间添加分隔符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27607694/

相关文章:

ios - 绘制椭圆渐变

ios 9 通过 HDMI 闪电适配器播放 mp4 全屏视频

ios - @synchronized block 与 GCD dispatch_async()

objective-c - NSString 从头开始​​获取 100 个字符

swift - 为什么 tabbarcontroller 中的窗口手势识别器不起作用?

ios - Xcode 4.2或4.6的iOS 7部署

ios - 从纵向模式转为横向模式时,屏幕上的图像位置不佳?

objective-c - Cocoa/Objective-C 中的后台(异步)编程

Swift 3D Touch 打开 TabBarController 而不是 RootViewController

ios - 与两个 UINavigationController 共享与 rootViewController 相同的 UIViewController