Skip to content

Instantly share code, notes, and snippets.

@IntegerOverlord
Last active October 31, 2022 13:39
Show Gist options
  • Save IntegerOverlord/e1b959c0132f527a9efcce108f16edf3 to your computer and use it in GitHub Desktop.
Save IntegerOverlord/e1b959c0132f527a9efcce108f16edf3 to your computer and use it in GitHub Desktop.
UIButton masksToBounds issue
class TestButton: UIButton {
init() {
super.init(frame: CGRect.zero)
self.layer.masksToBounds = true
self.layer.borderColor = UIColor.black.cgColor
self.layer.borderWidth = 1 / UIScreen.main.scale
self.layer.cornerCurve = .continuous
self.setTitle("Test", for: .normal)
self.setTitleColor(.black, for: .normal)
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
override func layoutSubviews() {
super.layoutSubviews()
self.layer.cornerRadius = self.bounds.height / 2.0
}
}
class TestViewController: UIViewController {
init() {
super.init(nibName: nil, bundle: nil)
}
required init?(coder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
override func viewDidLoad() {
super.viewDidLoad()
self.view.backgroundColor = .white
let testView = TestButton()
testView.translatesAutoresizingMaskIntoConstraints = false
self.view.addSubview(testView)
NSLayoutConstraint.activate([
self.view.centerXAnchor.constraint(equalTo: testView.centerXAnchor),
self.view.centerYAnchor.constraint(equalTo: testView.centerYAnchor),
testView.heightAnchor.constraint(equalToConstant: 50),
testView.widthAnchor.constraint(equalToConstant: 200)
])
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment