故事板上看不到自定义视图 (xib)
Custom view (xib) not visible on storyboard
我有一个自定义视图 xib
,其中有多个 TableViews
。我将其作为嵌套视图加载到 ViewController
中:
ViewController
|_ View
|_ View (This is where I load my Custom View)
当我 运行 我的应用程序时,我可以在屏幕上看到空表,所以我的 xib 正在正确加载。
到目前为止,一切都很好。
我的问题是,我无法在故事板中看到我的表格。所以我无法 ctrl+拖动并继续。
如何在故事板视图中查看我的自定义视图?
故事板只能包含 ViewController 和 Segue。如果你想从 ViewController 访问你的 xib 中的某些东西来设置委托等,那么你必须从代码中进行。例如,当目标 ViewController
在 Storyboard 中而 UITableView
在另一个 xib 中时,您不能在 IB 中设置 UITableView
的委托。
例如,我们有TopView(我的CustomView):
TopView.xib,在File's Owner[=16=中设置TopView class ]
TopView.swift
注意:不要忘记@IBDesignable 标签。
import UIKit
@IBDesignable
class TopView: UIView {
//MARK:- IB Outlets
var contentView:UIView?
//MARK:- Lifecycle
override func awakeFromNib() {
super.awakeFromNib()
}
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
setupThisView()
}
override init(frame: CGRect) {
super.init(frame: frame)
setupThisView()
}
override func prepareForInterfaceBuilder() {
super.prepareForInterfaceBuilder()
setupThisView()
contentView?.prepareForInterfaceBuilder()
}
//MARK:- Lifecycle methods
private func setupThisView(){
guard let view = loadViewFromNib() else { return }
view.frame = bounds
view.autoresizingMask = [.flexibleWidth, .flexibleHeight]
addSubview(view)
contentView = view
}
func loadViewFromNib() -> UIView? {
let nibName = String(describing: TopView.self)
let bundle = Bundle(for: type(of: self))
let nib = UINib(nibName: nibName, bundle: bundle)
return nib.instantiate(withOwner: self,options: nil).first as? UIView
}
}
3.Add UIView in Storyboard 并将视图的 class 设置为 顶视图
If the TopView has not yet appeared in Storyboard then (this happens usually):
- 启用编辑器 -> 自动刷新视图
- 单击编辑器 -> 刷新所有视图
Still?
- 清理项目:⌘ + K
- 构建项目 ⌘ + B
情节提要中的结果:
我有一个自定义视图 xib
,其中有多个 TableViews
。我将其作为嵌套视图加载到 ViewController
中:
ViewController
|_ View
|_ View (This is where I load my Custom View)
当我 运行 我的应用程序时,我可以在屏幕上看到空表,所以我的 xib 正在正确加载。
到目前为止,一切都很好。
我的问题是,我无法在故事板中看到我的表格。所以我无法 ctrl+拖动并继续。
如何在故事板视图中查看我的自定义视图?
故事板只能包含 ViewController 和 Segue。如果你想从 ViewController 访问你的 xib 中的某些东西来设置委托等,那么你必须从代码中进行。例如,当目标 ViewController
在 Storyboard 中而 UITableView
在另一个 xib 中时,您不能在 IB 中设置 UITableView
的委托。
例如,我们有TopView(我的CustomView):
TopView.xib,在File's Owner[=16=中设置TopView class ]
TopView.swift
注意:不要忘记@IBDesignable 标签。
import UIKit
@IBDesignable
class TopView: UIView {
//MARK:- IB Outlets
var contentView:UIView?
//MARK:- Lifecycle
override func awakeFromNib() {
super.awakeFromNib()
}
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
setupThisView()
}
override init(frame: CGRect) {
super.init(frame: frame)
setupThisView()
}
override func prepareForInterfaceBuilder() {
super.prepareForInterfaceBuilder()
setupThisView()
contentView?.prepareForInterfaceBuilder()
}
//MARK:- Lifecycle methods
private func setupThisView(){
guard let view = loadViewFromNib() else { return }
view.frame = bounds
view.autoresizingMask = [.flexibleWidth, .flexibleHeight]
addSubview(view)
contentView = view
}
func loadViewFromNib() -> UIView? {
let nibName = String(describing: TopView.self)
let bundle = Bundle(for: type(of: self))
let nib = UINib(nibName: nibName, bundle: bundle)
return nib.instantiate(withOwner: self,options: nil).first as? UIView
}
}
3.Add UIView in Storyboard 并将视图的 class 设置为 顶视图
If the TopView has not yet appeared in Storyboard then (this happens usually):
- 启用编辑器 -> 自动刷新视图
- 单击编辑器 -> 刷新所有视图
Still?
- 清理项目:⌘ + K
- 构建项目 ⌘ + B
情节提要中的结果: