swift 初始化UIView的Swift子类,在.xib中设计

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了swift 初始化UIView的Swift子类,在.xib中设计相关的知识,希望对你有一定的参考价值。

// Create CustomView.xib, set File's Owner to CustomView. 
// Link the top level view in the XIB to the contentView outlet.

class CustomView : UIView {
    @IBOutlet private var contentView:UIView?
    // other outlets
    
    override init(frame: CGRect) { // for using CustomView in code
        super.init(frame: frame)
        self.commonInit()
    }
    
    required init?(coder aDecoder: NSCoder) { // for using CustomView in IB
        super.init(coder: aDecoder)
        self.commonInit()
    }
    
    private func commonInit() {
        Bundle.main.loadNibNamed("CustomView", owner: self, options: nil)
        guard let content = contentView else { return }
        content.frame = self.bounds
        content.autoresizingMask = [.flexibleHeight, .flexibleWidth]
        self.addSubview(content)
    }
}

以上是关于swift 初始化UIView的Swift子类,在.xib中设计的主要内容,如果未能解决你的问题,请参考以下文章