如何为其中一个UITabBar项添加左右边框?

时间:2016-07-13 09:03:17

标签: ios swift uitabbarcontroller

如果你看下面的图片,你会看到两条垂直线 - 隔离中间标签项。 (线条很暗)

在代码中,我该如何创建这两行?

enter image description here

2 个答案:

答案 0 :(得分:1)

你可以:
1.使用.png中的行添加所选图像 2.使用线条设置背景图像,并根据所选标签更改此图像 3.创建自己的视图,继承自UIView并将其添加为子视图(在这种情况下,您必须实现自己的切换逻辑)

答案 1 :(得分:0)

 This will help you to add the seperators
//Add seperators Line
        if let items = self.tabBar.items {
            //Get the height of the tab bar
            let height = self.tabBar.bounds.height
            let width = self.tabBar.bounds.width

//Calculate the size of the items
            let numItems = CGFloat(items.count)
            let itemSize = CGSize(
                width: tabBar.frame.width / numItems,
                height: tabBar.frame.height)

            for (index, _) in items.enumerated() {
                //We don't want a separator on the left of the first item.
                if index > 0 {

//Xposition of the item
                    let xPosition = itemSize.width * CGFloat(index)

                    let separator = UIView(frame: CGRect(
                        x: xPosition, y: 0, width: 3.5, height: height))
                    separator.backgroundColor = UIColor.white
                    tabBar.insertSubview(separator, at: 1)
               }
           }

扩展UIImage {

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

}