最佳答案
这是如何实现的示例:
class CustomButton: UIButton {
override func layoutSubviews() {
super.layoutSubviews()
layer.sublayers?.filter({ $0.name == "DashedTopLine" }).map({ $0.removeFromSuperlayer() })
let shapeLayer = CAShapeLayer()
shapeLayer.name = "DashedTopLine"
shapeLayer.bounds = bounds
shapeLayer.position = CGPoint(x: frame.width / 2, y: frame.height / 2)
shapeLayer.fillColor = UIColor.clear.cgColor
shapeLayer.strokeColor = UIColor.red.cgColor
shapeLayer.lineWidth = 1
shapeLayer.lineDashPattern = [2, 2]
let path = CGMutablePath()
path.move(to: CGPoint.zero)
path.addLine(to: CGPoint(x: frame.width, y: 0))
shapeLayer.path = path
layer.addSublayer(shapeLayer)
}
}
关于ios - 在 iOS 中的按钮下添加虚线,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43318740/