我有一个名为class MyClass的类UIView,我想用XIB文件初始化.我不知道如何使用调用的xib文件初始化此类View.xib
class MyClass: UIView {
// what should I do here?
//init(coder aDecoder: NSCoder) {} ??
}
Run Code Online (Sandbox Code Playgroud) 我正在尝试加载我的XIB文件,UIView但我遇到了一些麻烦.我有所需的覆盖功能,但它们似乎崩溃了.说出这个错误,警告:
无法加载任何Objective-C类信息.这将显着降低可用类型信息的质量.
我想知道是否有人可以告诉我如何正确加载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);
}
}
Run Code Online (Sandbox Code Playgroud) func loadViewFromNib() -> UIView {
let bundle = NSBundle(forClass: self.dynamicType)
let nib = UINib(nibName: "JobsView", bundle: bundle)
let view = nib.instantiateWithOwner(self, options: nil)[0] as! UIView
return view
}
Run Code Online (Sandbox Code Playgroud)
我试图加载我的JobsView.xib文件.但是,如果没有更多信息,它会给我带来不良访问.我该如何解决这个错误?
我已经从File的所有者链接了这个类
我有一个可通过 nib/xib 文件重用的 UIView。我想加载它并填充一个 UITableViewCell,它将在一个自动调整大小的 UITableView 中使用。全部带有自动布局。
大多数效果很好,但似乎加载的 UIView 使用周围添加的约束来缩小 UITableViewCell 的 contentView。这对高度有好处,但我不想要这个宽度。
public func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell
{
let cellId = "RowView0002"
var cell = tableView.dequeueReusableCell(withIdentifier: cellId)
if cell == nil {
cell = UITableViewCell(style: UITableViewCellStyle.default, reuseIdentifier: cellId)
let subView = RowView(frame: cell!.frame)
cell!.contentView.attachViewWithConstraints(subView)
let _ = subView.viewLoadedFromNibAttached(name: cellId)
}
return cell!
}
override public func viewDidLoad() {
super.viewDidLoad()
tableView.delegate = self
tableView.dataSource = self
tableView.estimatedRowHeight = 40.0
tableView.rowHeight = UITableViewAutomaticDimension
}
extension …Run Code Online (Sandbox Code Playgroud)