【发布时间】:2020-06-07 18:06:20
【问题描述】:
我有一个包含两个 UITextfields 的 UIView,我想在两者之间创建一个 1.0 像素宽度的分隔符,但我无法让它工作。
我尝试覆盖draw func,但没有成功,我也尝试使用bezierPath和shapeLayer,但我没有UIView的框架,所以无法绘制bezierPath。
这是我拥有的绘制覆盖:
override func draw(_ rect: CGRect) {
super.draw(rect)
if let context = UIGraphicsGetCurrentContext() {
context.setStrokeColor(UIColor.black.cgColor)
context.setLineWidth(1)
context.move(to: CGPoint(x: bounds.width / 2, y: 0))
context.addLine(to: CGPoint(x: bounds.width / 2, y: bounds.height))
context.strokePath()
}
}
这是 bezierPath 函数:
private func drawSeparator() {
let path = UIBezierPath()
path.addLine(to: CGPoint(x: bounds.width / 2, y: 0))
path.move(to: CGPoint(x: bounds.width / 2, y: self.bounds.height))
let shape = CAShapeLayer()
shape.path = path.cgPath
shape.strokeColor = UIColor.appGray.cgColor
shape.lineWidth = 1.0
shape.fillColor = UIColor.appGray.cgColor
self.layer.addSublayer(shape)
}
我还尝试将 path 变量移动到类级别,并将其用作惰性 var 以尝试以这种方式获取框架/边界,但它也不起作用。
我正在尝试创建一个简单的垂直分隔符。
【问题讨论】:
标签: ios