我试图在View上添加一个虚线边框,该边框将始终位于视图的框架上,而不是位于bound上。
我的代码:
'-'
如果我们旋转view,我不想旋转View的Border。 它应该指出正在显示的空间。
期望
答案 0 :(得分:3)
您需要采取两种视图,即内部视图和外部视图。内部视图(旋转视图)应位于外部视图的子视图中。两个视图的框架必须相同。使用以下代码行-
@IBOutlet weak var viewOuter: UIView! //static outer view
@IBOutlet weak var viewInner: UIView! // View that will be use for rotation
func addDashedBorder() {
let color = UIColor.red.cgColor
let shapeLayer = CAShapeLayer()
let frameSize = viewInner.bounds.size
let shapeRect = CGRect(x:0 , y: 0, width: frameSize.width, height: frameSize.height)
shapeLayer.frame = shapeRect
shapeLayer.position = CGPoint(x: frameSize.width/2, y: frameSize.height/2)
shapeLayer.fillColor = UIColor.clear.cgColor
shapeLayer.strokeColor = color
shapeLayer.lineWidth = 2
shapeLayer.lineJoin = kCALineJoinRound
shapeLayer.lineDashPattern = [6,3]
shapeLayer.path = UIBezierPath(roundedRect: viewInner.frame, cornerRadius: 5).cgPath
self.viewOuter.layer.addSublayer(shapeLayer)
}