Skip to content

Instantly share code, notes, and snippets.

@matteocrippa
Last active May 12, 2017 13:54
Show Gist options
  • Select an option

  • Save matteocrippa/6b1272118b2e669d24f273cab961bb3a to your computer and use it in GitHub Desktop.

Select an option

Save matteocrippa/6b1272118b2e669d24f273cab961bb3a to your computer and use it in GitHub Desktop.
UIStackView add backgroundColor
// fix issue UIStackView that background doesn't work
private var bkgColor: UIColor?
override public var backgroundColor: UIColor? {
get { return bkgColor }
set {
bkgColor = newValue
setNeedsLayout()
}
}
private lazy var backgroundLayer: CAShapeLayer = {
let layer = CAShapeLayer()
self.layer.insertSublayer(layer, at: 0)
return layer
}()
override public func layoutSubviews() {
super.layoutSubviews()
backgroundLayer.path = UIBezierPath(rect: self.bounds).cgPath
backgroundLayer.fillColor = self.backgroundColor?.cgColor
}
@hvrmeer
Copy link

hvrmeer commented May 12, 2017

Actually you can loose the "super.layoutSubviews()" call. "layoutSubviews()" is a prototype (empty) method.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment