簡體   English   中英

在Storyboard ViewController中導入筆尖視圖

[英]Import Nib View in Storyboard ViewController

Swift 3-Xcode 8-iOS10

我有一個帶有UIView的xib文件。 我想在我的ViewController中導入該視圖。

我的Player.xib

在此處輸入圖片說明

在此處輸入圖片說明

而我的情節提要結果: 在此處輸入圖片說明

而我的班級:

@IBDesignable
class EmbedPlayerViewV2: UIView {

    @IBOutlet weak var subTitleLbl: UILabel!

    var view: UIView!

    override init(frame: CGRect) {
        super.init(frame: frame)
        loadViewFromNib()
    }
    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
        loadViewFromNib()
    }

    func loadViewFromNib() {
        let bundle = Bundle(for: type(of: self))
        let nib = UINib(nibName: "Player", bundle: bundle)
        self.view = nib.instantiate(withOwner: self, options: nil).first as! UIView
        self.addSubview(self.view)
    }

    override func layoutSubviews() {
        subTitleLbl.text = "LOADED"
        // ...
    }
    // ...
}

我已經解決了我的問題。

func loadViewFromNib() {
    let bundle = Bundle(for: type(of: self))
    let nib = UINib(nibName: "Player", bundle: bundle)
    self.view = nib.instantiate(withOwner: self, options: nil).first as! UIView
    self.addSubview(view)
    view.leadingAnchor.constraint(equalTo: self.leadingAnchor).isActive = true
    view.trailingAnchor.constraint(equalTo: self.trailingAnchor).isActive = true
    view.topAnchor.constraint(equalTo: self.topAnchor).isActive = true
    view.bottomAnchor.constraint(equalTo: self.bottomAnchor).isActive = true
}

坦克亞倫

暫無
暫無

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

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