【发布时间】:2015-12-07 19:49:42
【问题描述】:
任务是创建自定义 UIView,使用 Swift 从 .xib 文件加载 UI。我该怎么做?
我尝试使用代码做到这一点:
import UIKit
@IBDesignable class CustomView: UIView {
var view: UIView!
@IBOutlet weak var label: UILabel!
override init(frame: CGRect) {
super.init(frame: frame)
xibSetup()
}
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
xibSetup()
}
func xibSetup() {
view = loadViewFromNib()
view.frame = bounds
view.autoresizingMask = [UIViewAutoresizing.FlexibleWidth, UIViewAutoresizing.FlexibleHeight]
addSubview(view)
}
func loadViewFromNib() -> UIView {
let bundle = NSBundle(forClass: self.dynamicType)
let nib = UINib(nibName: "CustomView", bundle: bundle)
let view = nib.instantiateWithOwner(self, options: nil)[0] as! UIView
return view
}
}
它运行了,但由于 EXC_BAD_ACCESS 崩溃并显示消息:
warning: could not load any Objective-C class information. This will significantly reduce the quality of type information available.
实际上,我需要将此处给出的代码http://qnoid.com/2013/03/20/How-to-implement-a-reusable-UIView.html 转换为 Swift 2.0,但我不知道该怎么做。
【问题讨论】: