将 XIB 文件加载到 UIView Swift [英] Loading a XIB file to a UIView Swift

查看:39
本文介绍了将 XIB 文件加载到 UIView Swift的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am trying to load my XIB file into a UIView but I am having some trouble. I have the required override functions but they seem to be crashing. Saying this error, warning:

无法加载任何 Objective-C 类信息.这会显着降低可用类型信息的质量.

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

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

I was wondering if someone could show me how to properly load the XIB file into a 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);  
    }
}

推荐答案

我在我们的一个项目中使用了这个,可能对你有用

I uses this in one of our projects, might be useful to you

import UIKit

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

这篇关于将 XIB 文件加载到 UIView Swift的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆