Skip to content

Instantly share code, notes, and snippets.

@fcaldarelli
Created December 4, 2018 15:18
Show Gist options
  • Save fcaldarelli/b47c1cb4dfa75dd4f4ca94f402f4a72f to your computer and use it in GitHub Desktop.
Save fcaldarelli/b47c1cb4dfa75dd4f4ca94f402f4a72f to your computer and use it in GitHub Desktop.
Custom UIView Inspectable
import Foundation
import UIKit
@IBDesignable
class MyButton: UIButton {
// Connect the custom button to the custom class
@IBOutlet weak var view: UIButton!
override init(frame: CGRect) {
super.init(frame: frame)
setup()
}
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
setup()
}
func setup() {
view = loadViewFromNib() as! UIButton!
view.frame = bounds
view.autoresizingMask = [UIViewAutoresizing.flexibleWidth,
UIViewAutoresizing.flexibleHeight]
addSubview(view)
// Add our border here and every custom setup
view.layer.borderWidth = 2
view.layer.borderColor = UIColor.white.cgColor
}
func loadViewFromNib() -> UIView! {
let bundle = Bundle(for: type(of: self))
let nib = UINib(nibName: String(describing: type(of: self)), bundle: bundle)
let view = nib.instantiate(withOwner: self, options: nil)[0] as! UIButton
return view
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment