LoginSignup
5
3

More than 5 years have passed since last update.

[Swift] UIViewのxibからインスタンス生成

Posted at

Extension

extension UIView {
    static func instantiate() -> Self {
        let nibName = NSStringFromClass(self).componentsSeparatedByString(".")[1]
        let nib = UINib(nibName: nibName, bundle: nil)
        return nib.instantiateWithOwner(nil, options: nil)[0] as! Self
    }
}

使い方

let customView = CustomView.instantiate()
5
3
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
5
3