我需要在标签栏项目上添加两条垂直线。我怎么能做到这一点?谢谢。
添加下一个代码:
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
}
我的结果:
Best Answer-推荐答案 strong>
为此,您可以继承 UITabBarController 并在您想要的位置添加 UIImageView:
class TabBarController: UITabBarController {
var image: UIImageView?
override func viewDidLoad() {
super.viewDidLoad()
image = UIImageView(image: createImage(color: UIColor(red:0.18, green:0.66, blue:0.24, alpha:1.0), size: tabBarItemSize, lineHeight: 4))
tabBar.addSubview(image!)
}
func createImage(color: UIColor, size: CGSize, lineHeight: CGFloat) -> UIImage {
let rect: CGRect = CGRect(x: 0, y: size.height - lineHeight, width: size.width, height: lineHeight )
UIGraphicsBeginImageContextWithOptions(size, false, 0)
color.setFill()
UIRectFill(rect)
let image: UIImage = UIGraphicsGetImageFromCurrentImageContext()!
UIGraphicsEndImageContext()
return image
}
}
您需要根据需要对其进行修改并计算您希望它所在的位置,但这应该会有所帮助。我在 an article I wrote 中对此有所了解.
关于ios - 我如何在标签栏项目上添加自定义 View ,我们在Stack Overflow上找到一个类似的问题:
https://stackoverflow.com/questions/49050842/
|