如何使用Xib創(chuàng)建自定義UIView
新建一個空白View xib文件
關(guān)聯(lián)xib文件和code 文件
有兩種方式去關(guān)聯(lián)xib文件和代碼文件,第一種就是設(shè)置file's owner 的 custom class,另外一種就是在view那里去設(shè)置custom class
File's owner和View's custom class的區(qū)別
File's owner custom class | View's custom class |
---|---|
NSObject | UIView |
根據(jù)上圖也可以看到,F(xiàn)ile's owner 的custom class對應(yīng)是NSObject類型,而View‘s custom class是UIView類型。
使用View's custom class創(chuàng)建關(guān)聯(lián)
XibView.swift 里面的代碼
import UIKit
@IBDesignable
class XibView: UIView {
let nibName = "XibView"
override init(frame: CGRect) {
super.init(frame: frame)
print(#function)
commonInit()
}
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
print(#function)
}
func commonInit() {
guard let view = loadViewFromNib() else { return }
view.frame = self.bounds
addSubview(view)
}
func loadViewFromNib() -> UIView? {
print(#function)
guard let view = Bundle.main.loadNibNamed("XibView", owner: nil, options: nil)?.first as? UIView else { return nil }
return view
}
}
在ViewController中的使用
import UIKit
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
let xibView = XibView(frame: view.bounds)
view.addSubview(xibView)
}
}
運行?
可以看到先調(diào)用 init
去初始化,init
方法里面去調(diào)用loadNib
方法,而loadNib
會觸發(fā)required init?(coder aDecoder: NSCoder)
這個時候得說下required init?(coder aDecoder: NSCoder)
的調(diào)用,一般代碼去初始化一個view的時候,會走init(frame: CGRect)
,而不會走required init?(coder aDecoder: NSCoder)
. 需要去load Nib的時候才會調(diào)用required init?(coder aDecoder: NSCoder)
,也就是說用StoryBoard拉控件的形式只會調(diào)用required init?(coder aDecoder: NSCoder)
在StoryBoard中使用
1?? XibView.swift 里面需要修改部分代碼,即是在required init?(coder aDecoder: NSCoder)
里面調(diào)用私有方法commonInit()去loadNib
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
print(#function)
commonInit()
}
2?? 在StoryBoard拉一個UIView,將class設(shè)置成自定義view的類就可以引用了
Run -> ….. 運行?
? 所以!如果使用View's custom class去關(guān)聯(lián)xib和代碼文件,并且要在storyboard使用這個自定義的xib view的時候,我們必須在required init?(coder aDecoder: NSCoder)
里面去調(diào)用commonInit()
去load xib里面添加的控件(用storyboard拉控件的形式,不會走init(frame: CGRect)
),我們將會陷入初始化的死循環(huán)。
使用File's owner
Apple官方文檔給出關(guān)于File's owner的說明:
About the File’s Owner
One of the most important objects in a nib file is the File’s Owner object. Unlike interface objects, the File’s Owner object is a placeholder object that is not created when the nib file is loaded. Instead, you create this object in your code and pass it to the nib-loading code. The reason this object is so important is that it is the main link between your application code and the contents of the nib file. More specifically, it is the controller object that is responsible for the contents of the nib file.
In Xcode, you can create connections between the File’s Owner and the other interface objects in your nib file. When you load the nib file, the nib-loading code recreates these connections using the replacement object you specify. This allows your object to reference objects in the nib file and receive messages from the interface objects automatically.
文件中最重要的對象之一是File的Owner對象。與接口對象不同,F(xiàn)ile的Owner對象是一個占位符對象,在加載nib文件時不會創(chuàng)建占位符對象。相反,您在代碼中創(chuàng)建此對象并將其傳遞給nib加載代碼。這個對象如此重要的原因是它是應(yīng)用程序代碼和nib文件內(nèi)容之間的主要鏈接。更具體地說,它是控制器對象,負(fù)責(zé)nib文件的內(nèi)容。
在Xcode中,您可以在文件所有者和nib文件中的其他接口對象之間創(chuàng)建連接。加載nib文件時,nib加載代碼使用您指定的替換對象重新創(chuàng)建這些連接。這允許您的對象引用nib文件中的對象并自動從接口對象接收消息.
Bundle.main.loadNibNamed("XibView", owner: self, options: nil)?.first as? UIView
可以看到此時我們把初始化好的owner對象作為參數(shù)傳遞進去了。
在Demo中的使用
XibView.swift 文件只需要修改loadNib部分代碼
func loadViewFromNib() -> UIView? {
print(#function)
guard let view = Bundle.main.loadNibNamed("XibView", owner: self, options: nil)?.first as? UIView else { return nil }
return view
}
StoryBoard中還是拉一個UIView控件,className改為自定義類型。
運行?
可以在StoryBoard里面使用Xib自定義的View了 ??????
總結(jié)
File’s Owner 和 View custom class都是用于xib跟代碼文件建立關(guān)聯(lián)的。
在StoryBoard里面使用自定義xib時候,只會走required init?(coder aDecoder: NSCoder)
方法,所以需要在里面調(diào)用loadNib加載添加在xib上的控件。
使用File’s Owner,用代碼去loadNib的時候,不會走required init?(coder aDecoder: NSCoder)
方法。但是使用View custom class時候會,這也就是為什么使用View custom class時候造成死循環(huán)的原因。
所以,如果需要在StoryBoard里面使用Xib自定義的View,需要設(shè)置File’s Owner,而不是View custom class.