答案 0 :(得分:1)
这是如何实现的示例:
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)
}
}
答案 1 :(得分:1)
Just set the background color for the UIImageView like this (Swift 4):
var dottedLineView : UIImageView = {
let view = UIImageView()
view.contentMode = .scaleAspectFill
view.clipsToBounds = true
view.backgroundColor = UIColor(patternImage: #imageLiteral(resourceName: "dashedLine"))
return view
}()