如何使用 Swift 从其父级的笔尖创建视图
How to create view from its parent's nib using Swift
我觉得这个问题很简单,但经过长时间的搜索没有找到答案,我想在这里问一下:
我有一个带有 .xib 文件的自定义视图 (MyCustomView:UIView)。
要将此 class 与其笔尖一起使用,我添加了一个 class 函数,如下所示
class func addCustomViewToView(view: UIView)
{
let CV = NSBundle(forClass: MyCustomView.self).loadNibNamed("MyCustomView", owner: self, options: nil).first as! MyCustomView
CV.frame = CGRectMake(0, 0, view.frame.width, view.frame.height)
view.addSubview(CV)
}
在我的应用程序的另一个地方,我不得不扩展我的自定义视图,所以我创建了我的自定义视图的子类:
class MyExtendedCustomClass : MyCustomClass
{
...
}
我试图覆盖 class 函数,但我没有找到使用父项的 nib 文件创建子 class 项目的方法。
正确的做法是什么?
谢谢
尽管我问这个问题已经很久了,但我会添加我的解决方案,希望它能对某人有所帮助。
主要问题是当我调用函数时
addCustomViewToView(view: UIView)
在 subCustomView 中它创建了视图作为 superCustomView class 由于行:
let CV = NSBundle...loadNibNamed(...).first as! MyCustomView
所以...
首先,我初始化 CustomView 的方式:
var view:UIViwe!
override public init(frame: CGRect)
{
super.init(frame: frame)
self.commonInit()
}
required public init?(coder aDecoder: NSCoder)
{
super.init(coder: aDecoder)
self.commonInit()
}
private func commonInit()
{
let bundle = NSBundle(forClass: self.dynamicType)
let nib = UINib(nibName: "CustomView", bundle: myBundle)
self.view = nib.instantiateWithOwner(self, options: nil).first as! UIView
self.view.frame = self.bounds
self.view.autoresizingMask = [.FlexibleHeight, .FlexibleWidth]
//here you can add things to your view....
self.addSubview(self.view)
}
override public class func layerClass() -> AnyClass
{
return AVPlayerLayer.self
}
现在我可以使用
从代码中启动这个 class
init(frame: CGRect)
然后从情节提要中创建视图并将其定义为 class 作为我的自定义视图,在这种情况下将调用下一个初始化:
init?(coder aDecoder: NSCoder)
在那之后,在继承的视图中我不应该什么都不做,它 "init" 将从 CustomView class 调用 "super.init" 并且会毫无问题地初始化 nib 文件.
我觉得这个问题很简单,但经过长时间的搜索没有找到答案,我想在这里问一下:
我有一个带有 .xib 文件的自定义视图 (MyCustomView:UIView)。 要将此 class 与其笔尖一起使用,我添加了一个 class 函数,如下所示
class func addCustomViewToView(view: UIView)
{
let CV = NSBundle(forClass: MyCustomView.self).loadNibNamed("MyCustomView", owner: self, options: nil).first as! MyCustomView
CV.frame = CGRectMake(0, 0, view.frame.width, view.frame.height)
view.addSubview(CV)
}
在我的应用程序的另一个地方,我不得不扩展我的自定义视图,所以我创建了我的自定义视图的子类:
class MyExtendedCustomClass : MyCustomClass
{
...
}
我试图覆盖 class 函数,但我没有找到使用父项的 nib 文件创建子 class 项目的方法。
正确的做法是什么?
谢谢
尽管我问这个问题已经很久了,但我会添加我的解决方案,希望它能对某人有所帮助。
主要问题是当我调用函数时
addCustomViewToView(view: UIView)
在 subCustomView 中它创建了视图作为 superCustomView class 由于行:
let CV = NSBundle...loadNibNamed(...).first as! MyCustomView
所以...
首先,我初始化 CustomView 的方式:
var view:UIViwe!
override public init(frame: CGRect)
{
super.init(frame: frame)
self.commonInit()
}
required public init?(coder aDecoder: NSCoder)
{
super.init(coder: aDecoder)
self.commonInit()
}
private func commonInit()
{
let bundle = NSBundle(forClass: self.dynamicType)
let nib = UINib(nibName: "CustomView", bundle: myBundle)
self.view = nib.instantiateWithOwner(self, options: nil).first as! UIView
self.view.frame = self.bounds
self.view.autoresizingMask = [.FlexibleHeight, .FlexibleWidth]
//here you can add things to your view....
self.addSubview(self.view)
}
override public class func layerClass() -> AnyClass
{
return AVPlayerLayer.self
}
现在我可以使用
从代码中启动这个 classinit(frame: CGRect)
然后从情节提要中创建视图并将其定义为 class 作为我的自定义视图,在这种情况下将调用下一个初始化:
init?(coder aDecoder: NSCoder)
在那之后,在继承的视图中我不应该什么都不做,它 "init" 将从 CustomView class 调用 "super.init" 并且会毫无问题地初始化 nib 文件.