相关疑难解决方法(0)

如何在Swift中使用XIB文件初始化/实例化自定义UIView类

我有一个名为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)

ios swift

126
推荐指数
8
解决办法
14万
查看次数

将XIB文件加载到UIView Swift

我正在尝试加载我的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)

swift

21
推荐指数
6
解决办法
5万
查看次数

实例化Nib Bad Access Code = 2

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的所有者链接了这个类

swift

5
推荐指数
0
解决办法
1036
查看次数

在 UITableViewCell 中加载 UIView 的 Nib 文件不会拉伸

我有一个可通过 nib/xib 文件重用的 UIView。我想加载它并填充一个 UITableViewCell,它将在一个自动调整大小的 UITableView 中使用。全部带有自动布局。

大多数效果很好,但似乎加载的 UIView 使用周围添加的约束来缩小 UITableViewCell 的 contentView。这对高度有好处,但我不想要这个宽度。

UITableViewCell 的外观 忽略下面的灰色单元格,这只是一个选定的单元格。

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)

uitableview ios autolayout uitableviewautomaticdimension

3
推荐指数
1
解决办法
4362
查看次数