Created
December 4, 2018 15:18
-
-
Save fcaldarelli/b47c1cb4dfa75dd4f4ca94f402f4a72f to your computer and use it in GitHub Desktop.
Custom UIView Inspectable
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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