如何使用 xib 创建自定义视图
how to create a custom view using xib
我创建了一个可以在多个地方重复使用的自定义视图,我已将此 class 名称分配给视图的子视图 controller.Here 我完全使用 XIB's.while 运行 这个应用程序正在崩溃,并在“ Bundle.main.loadNibNamed("TripCancelAlert", owner: self, options: nil)”"Thread 1: EXC_BAD_ACCESS (code=2, address=0x7ffeee23be2c)" 行出现错误 "Thread 1: EXC_BAD_ACCESS (code=2, address=0x7ffeee23be2c)"
查看class:
class TripCancelAlert: UIView {
override init(frame: CGRect) {
super.init(frame: frame)
commonInit()
}
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
commonInit()
}
func commonInit() {
Bundle.main.loadNibNamed("TripCancelAlert", owner: self, options: nil)
// self.addSubview(contentView)
}
/*
// MARK: - Navigation
// In a storyboard-based application, you will often want to do a little preparation before navigation
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
// Get the new view controller using segue.destination.
// Pass the selected object to the new view controller.
}
*/
}
在超类下方找到您的视图。
请务必将您的 xib
命名为与您的 class
( MyView.xib / MyView
)
相同的名称
class NibView: UIView {
@IBOutlet var view: UIView!
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
commonInit()
}
override init(frame: CGRect) {
super.init(frame: frame)
commonInit()
}
func commonInit() {
guard let viewFromNib = Bundle.main.loadNibNamed(className, owner: self, options: nil)?.first as? UIView
else { fatalError("Could not load view from nib file.") }
view = viewFromNib
translatesAutoresizingMaskIntoConstraints = false
view.translatesAutoresizingMaskIntoConstraints = false
addPinnedSubview(view)
}
func addPinnedSubview(_ view: UIView, withInsets insets: UIEdgeInsets = .zero) {
addSubview(view)
let viewsDict: [String: UIView] = ["childView": view]
var metrics: [String: Any] = [:]
metrics["topSpace"] = insets.top
metrics["bottomSpace"] = insets.bottom
metrics["leftSpace"] = insets.left
metrics["rightSpace"] = insets.right
addConstraints(NSLayoutConstraint.constraints(withVisualFormat: "V:|-(topSpace)-[childView]-(bottomSpace)-|",
options: [],
metrics: metrics,
views: viewsDict))
addConstraints(NSLayoutConstraint.constraints(withVisualFormat: "H:|-(leftSpace)-[childView]-(rightSpace)-|",
options: [],
metrics: metrics,
views: viewsDict))
}
}
我创建了一个可以在多个地方重复使用的自定义视图,我已将此 class 名称分配给视图的子视图 controller.Here 我完全使用 XIB's.while 运行 这个应用程序正在崩溃,并在“ Bundle.main.loadNibNamed("TripCancelAlert", owner: self, options: nil)”"Thread 1: EXC_BAD_ACCESS (code=2, address=0x7ffeee23be2c)" 行出现错误 "Thread 1: EXC_BAD_ACCESS (code=2, address=0x7ffeee23be2c)"
查看class:
class TripCancelAlert: UIView {
override init(frame: CGRect) {
super.init(frame: frame)
commonInit()
}
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
commonInit()
}
func commonInit() {
Bundle.main.loadNibNamed("TripCancelAlert", owner: self, options: nil)
// self.addSubview(contentView)
}
/*
// MARK: - Navigation
// In a storyboard-based application, you will often want to do a little preparation before navigation
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
// Get the new view controller using segue.destination.
// Pass the selected object to the new view controller.
}
*/
}
在超类下方找到您的视图。
请务必将您的 xib
命名为与您的 class
( MyView.xib / MyView
)
class NibView: UIView {
@IBOutlet var view: UIView!
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
commonInit()
}
override init(frame: CGRect) {
super.init(frame: frame)
commonInit()
}
func commonInit() {
guard let viewFromNib = Bundle.main.loadNibNamed(className, owner: self, options: nil)?.first as? UIView
else { fatalError("Could not load view from nib file.") }
view = viewFromNib
translatesAutoresizingMaskIntoConstraints = false
view.translatesAutoresizingMaskIntoConstraints = false
addPinnedSubview(view)
}
func addPinnedSubview(_ view: UIView, withInsets insets: UIEdgeInsets = .zero) {
addSubview(view)
let viewsDict: [String: UIView] = ["childView": view]
var metrics: [String: Any] = [:]
metrics["topSpace"] = insets.top
metrics["bottomSpace"] = insets.bottom
metrics["leftSpace"] = insets.left
metrics["rightSpace"] = insets.right
addConstraints(NSLayoutConstraint.constraints(withVisualFormat: "V:|-(topSpace)-[childView]-(bottomSpace)-|",
options: [],
metrics: metrics,
views: viewsDict))
addConstraints(NSLayoutConstraint.constraints(withVisualFormat: "H:|-(leftSpace)-[childView]-(rightSpace)-|",
options: [],
metrics: metrics,
views: viewsDict))
}
}