Make to make rounded background UIButton in iOS

Issue #373

UIButton.contentEdgeInsets does not play well with Auto Layout, we need to use intrinsicContentSize

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
final class InsetButton: UIButton {
required init(text: String) {
super.init(frame: .zero)

titleLabel?.textColor = .white
setTitle(text, for: .normal)

layer.cornerRadius = 15
layer.masksToBounds = true
backgroundColor = .black
isUserInteractionEnabled = false
}

required init?(coder aDecoder: NSCoder) {
fatalError()
}

override var intrinsicContentSize: CGSize {
let size = super.intrinsicContentSize
return CGSize(width: size.width + 24, height: size.height)
}
}

Comments