You can simply create a IBDesignable class like this:
import UIKit
@IBDesignable
class BorderedView: UIView {
@IBInspectable var cornerRadius: CGFloat = 0
@IBInspectable var borderWidth: CGFloat = 0
@IBInspectable var borderColor: UIColor = UIColor.clear
override func draw(_ rect: CGRect) {
let path = UIBezierPath(roundedRect: rect, cornerRadius: cornerRadius)
path.lineWidth = borderWidth
borderColor.setStroke()
let dashPattern : [CGFloat] = [10, 4]
path.setLineDash(dashPattern, count: 2, phase: 0)
path.stroke()
}
}
Then just subclass your view with BorderedView from Xcode.
This way you can set the border color and border width very easily from the interface builder!
You can simply create a IBDesignable class like this:
Then just subclass your view with BorderedView from Xcode. This way you can set the border color and border width very easily from the interface builder!
} and call this function in viewdidLoad() with delay:
Swift 4.3
Based off rmooney's answer as a UIView extension with configurable parameters that have default values set.
If you want this to work with cornerRadius then try this
Another method if you like sublayers. In your custom view's init, put this (_border is an ivar):
And in your layoutsubviews, put this:
In Swift 3