簡體   English   中英

將Xib加載到UIView中而不將其添加為子視圖

[英]Load Xib into UIView without add it as a subview

如何將筆尖加載到UIView而不必將其添加為subview 在objc中,您可以在init方法中完成self = [NSBundle MainBundle].loadNibNamed…但是swift表示“Cannot assign to 'self' in a method”

在此處輸入圖片說明

在對象中,您可以執行此操作。 可重用的視圖,但是似乎並不喜歡它。 我發現的唯一方法是將Xib加載到UIView並將其添加為當前視圖的子視圖

@IBDesignable class CustomViewFromXib: UIView 
{
    var nibName: String = "CustomViewFromXib"
    override init(frame: CGRect) 
    {
        super.init(frame: frame)
        setup()
    }
    required init(coder aDecoder: NSCoder) 
    {
        super.init(coder: aDecoder)
        setup()
    }

    func setup() 
    {
        let bundle = NSBundle(forClass: self.dynamicType)
        let nib = UINib(nibName: nibName, bundle: bundle)
        let view = nib.instantiateWithOwner(self, options: nil)[0] as! UIView
        view.frame = bounds
        self.view.addSubview(view)
    }
}

[[NSBundle mainBundle] loadNibNamed:@“ myNib” owner:self options:nil];

是另一種方法並設置其所有者。

有點滑,可能會破壞KVO / KVC的一面

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM