Skip to content

Instantly share code, notes, and snippets.

@ergunkocak
Created October 29, 2019 20:56
Show Gist options
  • Save ergunkocak/53805f75b73300dd4da9864f6c6c427e to your computer and use it in GitHub Desktop.
Save ergunkocak/53805f75b73300dd4da9864f6c6c427e to your computer and use it in GitHub Desktop.
Basic UIView From xib file
//
// name the xib file as: "ViewExtendingNibLoadingView.xib"
//
// let newView = ViewExtendingNibLoadingView()
//
import UIKit
@IBDesignable
class NibLoadingView: UIView {
@IBOutlet weak var view: UIView!
override init(frame: CGRect) {
super.init(frame: frame)
nibSetup()
}
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
nibSetup()
}
private func nibSetup() {
backgroundColor = .clear
view = loadViewFromNib()
view.frame = bounds
view.autoresizingMask = [.flexibleWidth, .flexibleHeight]
view.translatesAutoresizingMaskIntoConstraints = true
addSubview(view)
}
private func loadViewFromNib() -> UIView {
let bundle = Bundle(for: type(of: self))
let nib = UINib(nibName: String(describing: type(of: self)), bundle: bundle)
let nibView = nib.instantiate(withOwner: self, options: nil).first as! UIView
return nibView
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment