extension UIView { //设置虚线边框 func setDashiedBorder() { let borderLayer = CAShapeLayer() borderLayer.bounds = self.bounds borderLayer.position = CGPointMake(self.bounds.size.width/2, self.bounds.size.height/2) borderLayer.path = UIBezierPath(roundedRect: borderLayer.bounds, cornerRadius: 0).CGPath borderLayer.lineWidth = 0.5 borderLayer.lineDashPattern = [2, 2] borderLayer.fillColor = UIColor.clearColor().CGColor borderLayer.strokeColor = UIColor(red).CGColor self.layer.addSublayer(borderLayer) } }