2016-07-14 69 views

回答

1
@IBDesignable class Menu: UIView { 

    var view: UIView! 

    override init(frame: CGRect) { 

     super.init(frame: frame) 
     xibSetup() 
    } 

    required init?(coder aDecoder: NSCoder) { 

     super.init(coder: aDecoder) 
     xibSetup() 
    } 

    func xibSetup() { 

     view = loadViewFromNib() 
     view.frame = bounds 
     view.autoresizingMask = [UIViewAutoresizing.FlexibleWidth, UIViewAutoresizing.FlexibleHeight] 
     addSubview(view) 
    } 

    func loadViewFromNib() -> UIView { 

     let bundle = NSBundle(forClass: self.dynamicType) 
     let nib = UINib(nibName: "Menu", bundle: bundle) 
     let view = nib.instantiateWithOwner(self, options: nil)[0] as! UIView 

     return view 
    } 

    etc etc ... 
} 

Nib Name

Nib Identity Inspector

然後一個UIView到您的Main.storyboard添加爲與Identity檢查器菜單類爲該視圖。

+0

爲什麼要將在筆尖中獲得的視圖添加爲子視圖而不僅僅是返回視圖?如果包含在NIB中的視圖碰巧是一個'Menu'視圖,它將陷入無限循環。 – redent84

相關問題