微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

将XIB文件加载到UIView Swift

我正在尝试将我的XIB文件加载到UIView中但我遇到了一些麻烦.我有所需的覆盖功能,但它们似乎崩溃了.说出这个错误,警告:

Could not load any Objective-C class information. This will
significantly reduce the quality of type information available.

我想知道是否有人可以告诉我如何将XIB文件正确加载到UIView中

import UIKit

class Widget: UIView {

    let view = UIView()

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

        //call function

        loadNib()

    }

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

        loadNib()

        //fatalError("init(coder:) has not been implemented")
    }

    func loadNib() {
        let bundle = NSBundle(forClass: self.dynamicType)
        let nib = UINib(nibName: "nib",bundle: bundle)
        let view = nib.instantiateWithOwner(self,options: nil)[0] as! UIView
        view.frame = bounds
        view.autoresizingMask = [.FlexibleWidth,.FlexibleHeight]
        self.addSubview(view);  
    }
}
我在我们的一个项目中使用它,maby对你有用
import UIKit

class RegisterPageView: UIView {

        class func instanceFromNib() -> RegisterPageView {
            return UINib(nibName: "RegisterPageView",bundle: nil).instantiateWithOwner(nil,options: nil)[0] as! RegisterPageView
        }
}

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐