删除标签栏项目之间的间距(左侧和右侧)

时间:2016-11-11 22:03:58

标签: ios swift uitabbaritem

如何在使用" fill"时删除标签栏项目之间的间距值"项目位置"选项?

我尝试过以下方法:

    let tabBarController = window!.rootViewController as! UITabBarController
    tabBarController.tabBar.itemSpacing = 0
    let numberOfItems = CGFloat(tabBarController.tabBar.items!.count)
    let tabBarItemSize = CGSize(width: tabBarController.tabBar.frame.width / numberOfItems, height: tabBarController.tabBar.frame.height)
    tabBarController.tabBar.selectionIndicatorImage = UIImage.imageWithColor(color: UIColor.secondaryHighlight(), size: tabBarItemSize).resizableImage(withCapInsets: UIEdgeInsets.zero)
    for item in tabBarController.tabBar.items! {
        item.imageInsets = UIEdgeInsetsMake(0, 0, 0, 0)
    }

extension UIImage {

    class func imageWithColor(color: UIColor, size: CGSize) -> UIImage {
        let rect: CGRect = CGRect(x: 0, y: 0, width: size.width, height: size.height)
        UIGraphicsBeginImageContextWithOptions(size, false, 0)
        color.setFill()
        UIRectFill(rect)
        let image: UIImage = UIGraphicsGetImageFromCurrentImageContext()!
        UIGraphicsEndImageContext()
        return image
    }

}

此结果始终如此(请注意第一个标签栏项左侧的绿线):

tab bar items

4 个答案:

答案 0 :(得分:1)

您也可以使用影响图像的标题偏移:

    tabBar.items!.first?.titlePositionAdjustment = UIOffsetMake(30.0, 0.0);
    tabBar.items!.last?.titlePositionAdjustment = UIOffsetMake(-30.0, 0.0);

在这种情况下,结果将是:
result screenshot

如何使用三个项目:
three items result screenshot

答案 1 :(得分:1)

    tabBarController?.tabBar.frame.origin.x = -2
    tabBarController?.tabBar.frame.size.width += 2

答案 2 :(得分:0)

对不起,迟到了。希望这有帮助!

tabBar.frame.size.width = self.view.frame.width + 4
tabBar.frame.origin.x = -2

答案 3 :(得分:0)

能够控制所有标签栏项目的位置“不仅是第一个和最后一个”你可以像这样使用items数组索引:

tabBar.items?[1].titlePositionAdjustment = UIOffsetMake(-15.0 , 0.0) tabBar.items?[2].titlePositionAdjustment = UIOffsetMake(15.0 , 0.0)