从nib实例化UIView会导致无限循环问题

问题描述 投票:0回答:1

我正在尝试使用我创建的自定义视图。

我使用来自nib的实例化,但它会导致无限循环,我不知道如何修复。任何的想法?

以下是运行结果的图像:

enter image description here

这是导致问题的代码:

// MARK:  - Init & Setup
// Needed for IBDesignable



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

required init?(coder aDecoder: NSCoder) {
    super.init(coder: aDecoder)
    setup()
}

func setup(){
    view = loadViewFromNib()
    view.frame = bounds
    view.autoresizingMask = UIViewAutoresizing(rawValue: UIViewAutoresizing.RawValue(UInt8(UIViewAutoresizing.flexibleWidth.rawValue) | UInt8(UIViewAutoresizing.flexibleHeight.rawValue)))
    addSubview(view)
}

func loadViewFromNib() -> UIView{
    let bundle = Bundle(for:type(of: self))
    let nib = UINib(nibName: "LoginView", bundle: bundle)         // TEST: changin bundle from bundle-> nil

    let view = nib.instantiate(withOwner: self, options: nil)[0] as! UIView
    return view
}

编辑:这是连接的图像

enter image description here

谢谢 :)

swift xcode swift3 custom-controls nib
1个回答
© www.soinside.com 2019 - 2024. All rights reserved.