Skip to content

Instantly share code, notes, and snippets.

@migueltg
Created May 31, 2018 15:10
Show Gist options
  • Save migueltg/69fbf7c75399d2ca3eecd50b839251c5 to your computer and use it in GitHub Desktop.
Save migueltg/69fbf7c75399d2ca3eecd50b839251c5 to your computer and use it in GitHub Desktop.
Load view from nib in xcode for storyboard and code
func loadViewFromNib(nibName: String? = "\(self)") {
guard let nibName = nibName else { return }
let bundle = Bundle(for: type(of: self))
let nib = UINib(nibName: nibName, bundle: bundle)
guard let view = nib.instantiate(withOwner: self, options: nil).first as? UIView else { return }
view.frame = bounds
view.autoresizingMask = [.flexibleWidth, .flexibleHeight]
addSubview(view)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment