Last active
May 2, 2020 07:13
-
-
Save chanonly123/3e8bdb539a9e9d2b148d7952273c7982 to your computer and use it in GitHub Desktop.
Instantiating custom UIView from xib
This file contains 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 UIKit | |
extension UIView { | |
static func initViewFromNib() -> Self { | |
let nibName = String(describing: self) | |
let index = 0 | |
let view = Bundle.main.loadNibNamed(nibName, owner: nil, options: nil)![index] | |
if let castedView = view as? Self { | |
return castedView | |
} else { | |
assertionFailure("Cannot cast to \(nibName)") | |
return Self() | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment