如何在标签栏项目上添加自定义视图

时间:2018-03-01 13:35:04

标签: ios swift tabbar

enter image description here

我需要在tabbar项目上添加两条垂直线。我怎么能做到这一点?感谢。

添加下一个代码:

var imageLeft: UIImageView?
var imageRight: UIImageView?

override func viewDidLoad() {
    super.viewDidLoad()

    let grayColor = UIColor(red: 170/255, green: 170/255, blue: 170/255, alpha: 1.0)
    let leftLine = (tabBar.frame.width/2) - ((tabBar.frame.width/5)/2)
    let rightLine = (tabBar.frame.width/2) + ((tabBar.frame.width/5)/2)
    imageLeft = UIImageView(image: createImage(color: grayColor, size: tabBar.frame.size, x: leftLine))
    imageRight = UIImageView(image: createImage(color: grayColor, size: tabBar.frame.size, x: rightLine))
    tabBar.addSubview(imageLeft!)
    tabBar.addSubview(imageRight!)
}

func createImage(color: UIColor, size: CGSize, x: CGFloat) -> UIImage {
    let rect: CGRect = CGRect(x: x, y: 5, width: 1, height: tabBar.frame.height - 11)
    UIGraphicsBeginImageContextWithOptions(size, false, 0)
    color.setFill()
    UIRectFill(rect)
    let image: UIImage = UIGraphicsGetImageFromCurrentImageContext()!
    UIGraphicsEndImageContext()
    return image
}

我的结果:

enter image description here

2 个答案:

答案 0 :(得分:2)

为此,您可以继承UITabBarController,并在您希望的位置添加UIImageView:

videoPlayer.controlledAudioTrackCount = 1;

您需要根据自己的需要对其进行修改并计算您希望的位置,但这应该有所帮助。我在an article I wrote中对此有了更多启发。

答案 1 :(得分:0)

您需要使用自定义标签栏。 这是一个关于如何制作一个的教程:https://github.com/codepath/ios_guides/wiki/Creating-a-Custom-Tab-Bar

或者你可以在Github上找到一个能为你做这件事的图书馆

相关问题