CAShapeLayer与UIVIew的偏移量';卡莱耶酒店

CAShapeLayer与UIVIew的偏移量';卡莱耶酒店,uiview,Uiview,我正在尝试向自定义UIViews层添加一个CAShapeLayer,这很顺利,除了它正在抵消自身 我最好用代码和截图来解释。。下面是: 我想要实现的只是一个简单的“斜杠”,就像Swift 3中的CAShapeLayer一样 在自定义UIView中,我有: var slashLayer: CAShapeLayer? var slashPath: CGPath? override func layoutSublayers(of layer: CALayer) { if layer

我正在尝试向自定义UIViews层添加一个CAShapeLayer,这很顺利,除了它正在抵消自身

我最好用代码和截图来解释。。下面是:

我想要实现的只是一个简单的“斜杠”,就像Swift 3中的CAShapeLayer一样

在自定义UIView中,我有:

    var slashLayer: CAShapeLayer?
var slashPath: CGPath?

override func layoutSublayers(of layer: CALayer) {
    if layer == self.layer {
        if slashLayer == nil {
            slashLayer = CAShapeLayer()
            slashLayer?.bounds = layer.bounds
            slashLayer?.strokeColor = UIColor.black.cgColor
            slashLayer?.fillColor = UIColor.black.cgColor
            slashLayer?.lineWidth = 3.0

            let slashPath = UIBezierPath()
            slashPath.move(to: CGPoint(x: layer.frame.minX, y: layer.frame.minY))
            slashPath.addLine(to: CGPoint(x: layer.frame.maxX, y: layer.frame.maxY))
            slashPath.close()

            slashLayer?.path = slashPath.cgPath

            layer.addSublayer(slashLayer!)
        }
    }
}
在UIView子类中就是这样

这就是我在Interface Builder(以及启动应用程序时)中得到的结果

由于CALayer.layoutManager已经不在了,我如何使层与层保持一致