LoginSignup
1
1

More than 5 years have passed since last update.

Swiftでxibファイルメモ

Last updated at Posted at 2018-09-02

nib(xib)ファイルから読み込みを行うExtension


extension UIView {
    func loadNib() {
        let view = Bundle.main.loadNibNamed(String(describing: type(of: self)), owner: self, options: nil)?.first as! UIView
        view.frame = self.bounds
        self.addSubview(view)
    }
}

こう使う


// クラス名はなんでも良し
class CustomView: UIView {

    // コードから初期化
    override init(frame: CGRect) {
        super.init(frame: frame)
        loadNib()
    }

    // Storyboardから初期化
    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
        loadNib()
    }   
}

これ自動的にやってくれないんですかね

1
1
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
1
1