Skip to content

Instantly share code, notes, and snippets.

@KentarouKanno
Last active July 7, 2016 10:57
Show Gist options
  • Save KentarouKanno/c3893bc9db01a9474d3f to your computer and use it in GitHub Desktop.
Save KentarouKanno/c3893bc9db01a9474d3f to your computer and use it in GitHub Desktop.
xib

xib

★ xibファイルから読み込んだUIViewを直接使用する

  • Viewを乗せるだけならxibを作成して呼び出すだけでOK
let customV = UINib(nibName: "CustomView", bundle: nil).instantiateWithOwner(self, options: nil).first as! UIView
self.view.addSubview(customV)

★ ViewControllreのViewをCustomViewに置き換える

import UIKit

class ViewController: UIViewController {

    override func loadView() {
        super.loadView()
        
        let customView = UINib(nibName: "CustomView", bundle: nil).instantiateWithOwner(self, options: nil).first as! CustomView
        customView.frame = view.frame
        self.view = customView
        
    }
}

★ UIViewのCustomClassをCustomViewクラスに設定した場合:1

// Code生成、Storyboard(xib)生成どちらもOK

import UIKit

class CustomView: UIView {
    
    required init(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)!

        generateFromNib()
    }
    
    override init(frame: CGRect) {
        super.init(frame: frame)

        generateFromNib()
    }
    
    @IBAction func pushButton(sender: UIButton) {
        print("Push")
    }
    
    func generateFromNib(){
        if self.subviews.count == 0 {
            let view = NSBundle.mainBundle().loadNibNamed("CustomView", owner: self, options: nil).first as! UIView
            view.frame = self.bounds
            self.addSubview(view)
        }
    }
}

// Usage
let customV = CustomView(frame: CGRectMake(0, 0, 200, 200))
customV.center = view.center
view.addSubview(customV)

★ UIViewのCustomClassをCustomViewクラスに設定した場合:2

// Code生成のみOK

import UIKit

class CustomView: UIView {
    
    required init(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)!
    }
    
    override func awakeFromNib() {
        // 初期化
    }
    
    @IBAction func pushButton(sender: UIButton) {
        print("Push")
    }
    
    class func loadView() -> CustomView  {
        return NSBundle.mainBundle().loadNibNamed("CustomView", owner: self, options: nil).first as! CustomView
    }
}

// Usage
let customV = CustomView.loadView()
customV.center = view.center
view.addSubview(customV)

★ File's OwnerをCustomViewクラスに設定した場合

// Code生成、Storyboard(xib)生成どちらもOK

class CustomView: UIView {
    
    required init(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)!
        // xib生成時
        generateFromNib()
    }
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        // code生成時
        generateFromNib()
    }
    
    @IBAction func pushButton(sender: UIButton) {
        print("Push")
    }
    
    func generateFromNib(){
        let view = NSBundle.mainBundle().loadNibNamed("CustomView", owner: self, options: nil).first as! UIView
        view.frame = self.bounds
        self.addSubview(view)
    }
}

// Usage
let customV = CustomView(frame: CGRectMake(0, 0, 200, 200))
customV.center = view.center
view.addSubview(customV)

★ ViewControllerのswiftファイル生成時にxibも一緒に生成した場合の呼びだし image

@IBAction func next(sender: UIButton) {
    
    // コードで生成する書き方でxibとも結びつく
    let nextVC = NextViewController()
    self.navigationController?.pushViewController(nextVC, animated: true)
}

★ 合わせて@IBDesignable & @IBInspectableを使用する場合

@IBDesignable
class CustomViewA: UIView {
    
    @IBOutlet weak var button: UIButton!
    
    @IBInspectable var buttonTitle: String = "" {
        didSet {
            button.setTitle(buttonTitle, forState: .Normal)
        }
    }
    
    required init(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)!
        
        generateFromNib()
    }
    
    override func awakeFromNib() {
        // 初期化
    }
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        generateFromNib()
    }
    
    @IBAction func pushButton(sender: UIButton) {
        print("Push")
    }
    
    func generateFromNib(){
        
        // @IBDesignableを使用する場合、nibからの生成はこの書き方の方がエラーにならない
        let bundle = NSBundle(forClass: self.dynamicType)
        let nib = UINib(nibName: "CustomViewA", bundle: bundle)
        let view = nib.instantiateWithOwner(self, options: nil).first as! UIView
        self.addSubview(view)
    }
}

@IBDesignable & @IBInspectable Image

image

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment