我正在尝试在界面生成器中配置自定义 UIView
并获取要呈现的 View 。每次我尝试使用自定义 Xib 执行此操作时,我都可以让 xib 在界面生成器中看起来很好,但是当我尝试将我的 MyCustomView
放入 UIVewController
时在界面生成器中,我遇到了各种编译器错误。
我看过的每个教程都是这样的:“只需在你的类声明前面添加 @IBDesignable
并将 @IBInspectable
放在你的变量前面,然后然后观看魔法发生”(例如:WWDC 2015 video)。或者它是在 Objective-C 中,或者它是一个 30 分钟的带有中文字幕的阿拉姆语视频等。
我的最终目标是能够拖入一个 UIView,在身份检查器中分配我的自定义类,并让 xib 的内容填充我拖入的 UIView 并能够调整 @IBInspectable
属性.
我在哪里寻找答案:
SO Post: @IBDesignable crashing agent
我遇到的错误:
Base.lproj/Main.storyboard: error: IB Designables: Failed to render and update auto layout status for PlaylistViewController (o1G-QR-OwX): The agent crashed
Base.lproj/Main.storyboard: error: IB Designables: Failed to update auto layout status: The agent crashed
我的 CurrentlyPlayingView
的身份检查器:
我想要出现在上述 Storyboard底部的 View :
我的代码:
import UIKit
@IBDesignable class CurrentlyPlayingView: UIView {
var view = UIView()
@IBOutlet weak var titleLabel: UILabel!
@IBOutlet weak var subtitleLabel: UILabel!
@IBOutlet weak var progressSlider: UISlider!
// IBInspectable attributes
@IBInspectable public var titleLabelColor: UIColor = .black {
didSet {
self.titleLabel.textColor = titleLabelColor
}
}
@IBInspectable public var subtitleLabelColor: UIColor = .black {
didSet {
self.subtitleLabel.textColor = subtitleLabelColor
}
}
@IBInspectable public var sliderMinColor: UIColor = .blue {
didSet {
self.progressSlider.minimumTrackTintColor = sliderMinColor
}
}
@IBInspectable public var sliderMaxColor: UIColor = .red {
didSet {
self.progressSlider.maximumTrackTintColor = sliderMaxColor
}
}
// MARK: - Initialization
override init(frame: CGRect) {
super.init(frame: frame)
xibSetup()
initialConfig()
}
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
xibSetup()
initialConfig()
}
private func initialConfig() {
self.titleLabel.textColor = titleLabelColor
self.subtitleLabel.textColor = subtitleLabelColor
self.progressSlider.minimumTrackTintColor = sliderMinColor
self.progressSlider.maximumTrackTintColor = sliderMaxColor
}
// MARK: - Nib handlers
private func xibSetup() {
view = loadViewFromNib()
view.frame = bounds
// Make the view stretch with containing view
view.autoresizingMask = [UIViewAutoresizing.flexibleWidth, UIViewAutoresizing.flexibleHeight]
// Adding custom subview on top of our view (over any custom drawing > see note below)
addSubview(view)
}
private func loadViewFromNib() -> UIView {
let bundle = Bundle(for: type(of: self))
let nib = UINib(nibName: "CurrentlyPlayingView", bundle: bundle)
// Assumes UIView is top level and only object in CustomView.xib file
let view = nib.instantiate(withOwner: self, options: nil).first as! UIView
return view
}
}
如果您已经读到这里,感谢您的阅读。如果你看到我遗漏了什么,我欢迎你的建议。
最佳答案
我认为这是因为您缺少 prepareForInterfaceBuilder
函数。
像这样重写这个函数。
override func prepareForInterfaceBuilder() {
super.prepareForInterfaceBuilder()
//setup your view with the settings
}
OP 的附录:
将 UIView
拖出到 YourViewController
并将其在 Identity Inspector 中的自定义类设置为 YourCustomView
后,确保“Inherit from Target”是勾选(从 Xcode 8.x 开始)。
关于ios - 让@IBDesignable UIView 在不崩溃 Interface Builder 的情况下进行绘制,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44234112/