如何在 Swift2 中更改 CGRect 的边框样式?

How to change border style of CGRect in Swift2?

我正在学习 Swift 我的设计师给了我这样的屏幕。

我遇到了如何更改此虚线形式的总学分 CGRect 的边框样式的问题。

我的代码是:

override func viewDidLoad()
{
    let promobox =  UIView()

    promobox.frame = CGRectMake(16, promotextfield.frame.minY + 180, self.view.frame.width - 32, 60)
    promobox.layer.borderColor = UIColor(red: 0, green: 0, blue: 0, alpha: 0.2).CGColor
    promobox.layer.borderWidth = 2.0
    promobox.layer.backgroundColor = UIColor(red: 255, green: 255, blue: 255, alpha: 0.2).CGColor

    //background color of box
    self.view.addSubview(promobox)
}

您可以这样做:

let promobox =  UIView()

promobox.frame = CGRectMake(16, 50, self.view.frame.width - 32, 60)
promobox.layer.borderColor = UIColor.whiteColor().CGColor
promobox.layer.borderWidth = 2.0
promobox.layer.backgroundColor = UIColor(red: 255, green: 255, blue: 255, alpha: 0.2).CGColor

let border = CAShapeLayer.init()
border.frame = promobox.bounds
border.path = UIBezierPath(rect: border.frame).CGPath
border.lineWidth = 2
border.strokeColor = UIColor(red: 0, green: 0, blue: 0, alpha: 0.2).CGColor
border.fillColor = UIColor.clearColor().CGColor
border.lineDashPattern = [4, 6]
promobox.layer.addSublayer(border)

玩转 lineDashPattern 以满足您的设计师的期望。