在Swift中将xib分配给UIView

61

在Objective-C中,可以在init方法中完成此操作:

-(id)init{
    self = [[[NSBundle mainBundle] loadNibNamed:@"ViewBtnWishList" owner:0 options:nil]     objectAtIndex:0];
return self;
}

但是当我在Swift中这样做时

init(frame: CGRect) {
    self = NSBundle.mainBundle().loadNibNamed("ViewDetailMenu", owner: 0, options: nil)[0] as? UIView
}

在方法中不能将自身赋值错误被显示。 现在我的解决方案是创建一个视图,并将从nib加载的视图添加到其中。 是否有更好的想法?

无法在方法中将自身分配给变量,出现错误提示。 我的解决方案是创建一个视图,并将从nib加载的视图添加到其中。 是否有更好的建议?


1
使用 UINib 会更加高效... 不过,在 Swift 中无法分配 self。你可能需要创建一个类方法,使用 xib 文件初始化你的类。 - holex
9个回答

131

针对Swift 4

extension UIView {
    class func loadFromNibNamed(nibNamed: String, bundle: Bundle? = nil) -> UIView? {
      return UINib(
          nibName: nibNamed,
          bundle: bundle
      ).instantiate(withOwner: nil, options: nil)[0] as? UIView
  }
}

针对Swift 3

您可以在UIView上创建一个扩展:

extension UIView {
    class func loadFromNibNamed(nibNamed: String, bundle: NSBundle? = nil) -> UIView? {
        return UINib(
            nibName: nibNamed,
            bundle: bundle
        ).instantiateWithOwner(nil, options: nil)[0] as? UIView
    }
}

注意:使用 UINib 更快,因为它会为您进行缓存。

然后您只需要执行以下操作:

ViewDetailItem.loadFromNibNamed("ViewBtnWishList")

你将能够在任何视图上重复使用该方法。


你为什么要使用比原来慢3-4倍的NSBundle来加载一个nib文件? - holex
2
感谢,那我可以按照以下方式创建自定义的UI视图吗? var customView = ViewDetailItem.loadFromNibNamed("ViewBtnWishList")但是如果我想在自定义视图创建时初始化一些更多的变量怎么办?在这种情况下,我们是不是就绕过了UIView的初始化方法了? - Siu Chung Chan
3
如果我在包含其他视图的布局中使用这个类,并且我将插座与该类连接,我会遇到问题。我会得到“Terminating app due to uncaught exception 'NSUnknownKeyException',reason: '[<NSObject 0x17000bae0> setValue:forUndefinedKey:]: this class is not key value coding-compliant for the key infoView.'” 的错误提示。 - beretis
能否在UIView本身内部加载nib文件? - Bagusflyer
1
只是好奇 - 为什么要使用class func而不是static func? - Pradeep Banavara
显示剩余13条评论

24

这对我起作用了。

override func awakeAfterUsingCoder(aDecoder: NSCoder) -> AnyObject? {
    if self.subviews.count == 0 {
        return loadNib()
    }
    return self
}

private func loadNib() -> YourCustomView {
    return NSBundle.mainBundle().loadNibNamed("YourCustomViewNibName", owner: nil, options: nil)[0] as YourCustomView
}

2
这是在Storyboard或XIB文件中重复使用的最佳解决方案。如果您只是将加载的nib视图添加为子视图,则委托等属性不会正确设置。 - Austin
请确保如果您有任何插座,将它们连接到XIB中的View而不是File Owner,否则您将会遇到KVO兼容性错误。 - Dov
这是目前为止唯一对我有效的。谢谢! - tyegah123
1
任何情况下最合适的解决方案。我不知道为什么在我发现它之前只有10个赞。 - WeZZard
1
但不幸的是,当使用Storyboard时,这个解决方案确实替换了Storyboard中原本的对象,导致您的视图约束或Storyboard完成的任何其他设置都会消失。 - WeZZard

24

在 Xcode 7 beta 4 和 Swift 2.0 中测试过。 以下代码将 xib 分配给 UIView。 您可以在 storyboard 中使用此自定义 xib 视图,并访问 IBOutlet 对象。

import UIKit

@IBDesignable class SimpleCustomView:UIView
{
    var view:UIView!;

    @IBOutlet weak var lblTitle: UILabel!

   @IBInspectable var lblTitleText : String?
        {
        get{
            return lblTitle.text;
        }
        set(lblTitleText)
        {
            lblTitle.text = lblTitleText!;
        }
    }

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

    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
        loadViewFromNib ()
    }
    func loadViewFromNib() {
        let bundle = NSBundle(forClass: self.dynamicType)
        let nib = UINib(nibName: "SimpleCustomView", bundle: bundle)
        let view = nib.instantiateWithOwner(self, options: nil)[0] as! UIView
        view.frame = bounds
        view.autoresizingMask = [.FlexibleWidth, .FlexibleHeight]
        self.addSubview(view);



    }


}

以编程方式访问自定义视图

self.customView =  SimpleCustomView(frame: CGRectMake(100, 100, 200, 200))
        self.view.addSubview(self.customView!);

源代码 - https://github.com/karthikprabhuA/CustomXIBSwift


3
在其他的XIB中也可以使用 - 这很棒。但是有一个问题,这并没有直接从XIB创建自定义视图。它是从XIB加载一个UIView并将其作为子视图添加到自定义视图中。 - n13
一份非常出色的贡献。非常清晰和完整。感谢您将其与Github仓库一起发布!一个小问题是SimpleCustomView.swift中的成员变量“view”似乎是不必要且未使用的。 - Biodave
其中一个解决方案是不从静态方法设置视图。 - hamsternik

18

这可能是你的解决方案:

Swift 3.x

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

Swift 2.x

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

1
完美的答案。简短、精练,而且非常有效。谢谢! - kakubei
我应该将这段代码放在XIB的视图控制器中还是我正在尝试添加XIB的视图控制器中? - MarksCode
@MarksCode,您需要将此代码片段添加到UIView类的子集中;这与此上下文中的任何视图控制器无关。 - holex

3

我认为这是一种最简单而且最干净的方法,来为一个UIView分配xib。Xcode 7.3和swift 2.0。

import UIKit

//Create CustomView class
class CustomView: UIView {

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

//Use it
let customView = CustomView.instanceFromNib() as! CustomView

请添加更多信息。仅包含代码和“试试这个”答案是不被鼓励的(因为它们不包含可搜索的内容,并且没有解释为什么某人应该“试试这个”)。我们在这里努力成为知识资源。 - Mogsdad

1
真正的Swift方法是使用协议和协议扩展。

我这样使用它:首先创建一个协议

    protocol XibInitializable {
        static var name: String { get }
        static var bundle: Bundle? { get }

        static func fromXib() -> Self
    }

然后我使用协议扩展来创建该协议的默认实现。
extension XibInitializable where Self : UIView {
    static var name: String {
        return String(describing: Self.self)
    }

    static var bundle: Bundle? {
        return nil
    }

    static func fromXib() -> Self {
        return UINib(nibName: name, bundle: bundle).instantiate(withOwner: nil, options: nil)[0] as! Self
    }
}

我们的协议实现已经完成。
为了使该协议正常工作,您需要确保xib文件的名称和类名相同。例如,例如。

enter image description here

最后添加协议并将您的类设为“final”,就像这里一样。

enter image description here

就是这样

并使用

enter image description here


我唯一想改变的是将 bundle 属性作为参数移入函数中,并将其默认值设置为 nil。我还会使用 nibName 而不是 name - jjatie

0

不必为 UIView 添加扩展,您可以定义一个协议并将实现添加到协议扩展中。然后,您可以声明 UIView 符合该协议。

这样可以使返回类型为 Self 而不是 UIView。因此,调用者无需转换为类。

在此处解释: https://dev59.com/XF8e5IYBdhLWcg3wqryt#33424509

import UIKit

protocol UIViewLoading {}
extension UIView : UIViewLoading {}

extension UIViewLoading where Self : UIView {

  // note that this method returns an instance of type `Self`, rather than UIView
  static func loadFromNib() -> Self {
    let nibName = "\(self)".characters.split{$0 == "."}.map(String.init).last!
    let nib = UINib(nibName: nibName, bundle: nil)
    return nib.instantiateWithOwner(self, options: nil).first as! Self
  }

}

0
刚刚创建了一个 UINib 扩展,用于从 xib 加载视图并使用约束嵌入到容器视图中,使用泛型和强命名(不使用字符串,假设您的 xib 文件名与实现文件名相同):
extension UINib {

    static func instantiateViewAndEmbedWithConstraints <T: UIView> (viewType viewType: T.Type, embedInto containerView: UIView) -> T {
        let view = UINib(nibName: String(viewType), bundle: nil).instantiateWithOwner(nil, options: nil).first as! T
        containerView.addSubview(view)
        view.translatesAutoresizingMaskIntoConstraints = false
        NSLayoutConstraint(item: view, attribute: .Leading, relatedBy: .Equal, toItem: containerView, attribute: .Leading, multiplier: 1, constant: 0).active = true
        NSLayoutConstraint(item: view, attribute: .Trailing, relatedBy: .Equal, toItem: containerView, attribute: .Trailing, multiplier: 1, constant: 0).active = true
        NSLayoutConstraint(item: view, attribute: .Top, relatedBy: .Equal, toItem: containerView, attribute: .Top, multiplier: 1, constant: 0).active = true
        NSLayoutConstraint(item: view, attribute: .Bottom, relatedBy: .Equal, toItem: containerView, attribute: .Bottom, multiplier: 1, constant: 0).active = true
        return view
    }

}

使用方法:

...outlets...
@IBOutlet var containerView: UIView!
var customView: CustomView!

...viewDidLoad...
customView = UINib.instantiateViewAndEmbedWithConstraints(viewType: CustomView.self, embedInto: containerView)

0

只需继承这个简单的类(Swift 5):

open class NibView: UIView {

    open override func awakeAfter(using coder: NSCoder) -> Any? {
        if subviews.count == 0 {
            return UINib(nibName: "\(Self.self)", bundle: nil).instantiate(withOwner: nil, options: nil)[0] as! UIView
        }
        return self
    }
}

class CustomView: NibView {
}

正如其他人所指出的那样,将文件的所有者设置为你的CustomView类(而不是xib的根视图本身)。然后将自定义类设置为任何你想要用你的自定义视图类替换的视图CustomView。此外,自动布局会尊重xib中所有约束条件,至少作为UITableViewCell内容视图的子视图。其他情况不确定。

就像这样简单,某种程度上苹果公司为我们做了另一个基础事物的任务!真是一家美妙的公司!永远不会因他们而感到无聊!


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接