Skip to content

Instantly share code, notes, and snippets.

Last active Jan 31, 2018
What would you like to do?
Creating a custom UIView
import UIKit
// MARK: TestViewDelegate
protocol TestViewDelegate: class {
func testView(_ testView: TestView, didSelect button: UIButton)
// MARK: TestView
class TestView: UIView {
// MARK: Delegates
weak var delegate: TestViewDelegate?
// MARK: Outlets
@IBOutlet var contentView: UIView!
@IBOutlet var button: UIButton!
// MARK: UIView Methods
override func awakeFromNib() {
// MARK: Action Methods
@IBAction func buttonPressed(sender: Any?) {
guard let button = sender as? UIButton else {
delegate?.testView(self, didSelect: button)
// MARK: Helper Methods
private func setupButton() {
button.backgroundColor = .red
button.setTitleColor(.white, for: .normal)
button.layer.cornerRadius = 5
button.clipsToBounds = true
// MARK: Init Methods
override init(frame: CGRect) {
super.init(frame: frame)
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
private func commonInit() {
Bundle.main.loadNibNamed(String(describing: TestView.self), owner: self, options: nil)
contentView.frame = self.bounds
contentView.autoresizingMask = [.flexibleWidth, .flexibleHeight]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment