Skip to content

Instantly share code, notes, and snippets.

@rtking1993
Last active January 31, 2018 16:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rtking1993/6cce0b7629c76bb113484c9754553d30 to your computer and use it in GitHub Desktop.
Save rtking1993/6cce0b7629c76bb113484c9754553d30 to your computer and use it in GitHub Desktop.
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() {
super.awakeFromNib()
setupButton()
}
// MARK: Action Methods
@IBAction func buttonPressed(sender: Any?) {
guard let button = sender as? UIButton else {
return
}
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)
commonInit()
}
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
commonInit()
}
private func commonInit() {
Bundle.main.loadNibNamed(String(describing: TestView.self), owner: self, options: nil)
addSubview(contentView)
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