Created
October 29, 2019 20:56
-
-
Save ergunkocak/53805f75b73300dd4da9864f6c6c427e to your computer and use it in GitHub Desktop.
Basic UIView From xib file
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
// | |
// 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