如何解决无法呈现和更新自动布局状态?在 xcode 10.2.1 中向自定义控件添加约束后
How do I resolve Failed to render and update auto layout status? after adding constraint to custom control in xcode 10.2.1
我有以下自定义控件:
import UIKit
import os.log
@IBDesignable class LocationControl: UIStackView {
@IBInspectable var nameSize: CGSize = CGSize(width: 100.0, height: 21.0) {
didSet {
setupLabels()
}
}
override init(frame: CGRect) {
super.init(frame: frame)
setupLabels()
}
required init(coder: NSCoder) {
super.init(coder: coder)
setupLabels()
}
private func setupLabels() {
// Create the labels
let nameLabel = UILabel()
nameLabel.text = "Name"
nameLabel.backgroundColor = UIColor.red
let textLabel = UILabel()
textLabel.text = "Text"
textLabel.backgroundColor = UIColor.green
// Add constraints
let margins = self.layoutMarginsGuide
nameLabel.translatesAutoresizingMaskIntoConstraints = false
nameLabel.heightAnchor.constraint(equalToConstant: nameSize.height).isActive = true
nameLabel.widthAnchor.constraint(equalToConstant: nameSize.width).isActive = true
// nameLabel.leadingAnchor.constraint(equalTo: margins.leadingAnchor).isActive = true
self.addSubview(nameLabel)
self.addSubview(textLabel)
}
}
此代码可以正常运行(但未达到预期效果)。如果我取消注释行
// nameLabel.leadingAnchor.constraint(equalTo: margins.leadingAnchor).isActive = true
然后在标准编辑器中切换到Main.storyboard,我得到以下错误:
file:///Users/tim.daley/AixNPanes/iOS/location/location/Base.lproj/Main.storyboard:错误:IB Designables:无法呈现和更新 UIViewController 的自动布局状态(29D-8H-aV6 ): 代理抛出异常。
我是 运行 Xcode macOS Mojave 10.14.5 上的版本 10.2.1 (10E1001)
您可能需要在约束之前添加视图
self.addSubview(nameLabel)
self.addSubview(textLabel)
nameLabel.translatesAutoresizingMaskIntoConstraints = false
NSLayoutConstraint.activate([
nameLabel.heightAnchor.constraint(equalToConstant: nameSize.height),
nameLabel.widthAnchor.constraint(equalToConstant: nameSize.width),
nameLabel.leadingAnchor.constraint(equalTo: margins.leadingAnchor)
])
我有以下自定义控件:
import UIKit
import os.log
@IBDesignable class LocationControl: UIStackView {
@IBInspectable var nameSize: CGSize = CGSize(width: 100.0, height: 21.0) {
didSet {
setupLabels()
}
}
override init(frame: CGRect) {
super.init(frame: frame)
setupLabels()
}
required init(coder: NSCoder) {
super.init(coder: coder)
setupLabels()
}
private func setupLabels() {
// Create the labels
let nameLabel = UILabel()
nameLabel.text = "Name"
nameLabel.backgroundColor = UIColor.red
let textLabel = UILabel()
textLabel.text = "Text"
textLabel.backgroundColor = UIColor.green
// Add constraints
let margins = self.layoutMarginsGuide
nameLabel.translatesAutoresizingMaskIntoConstraints = false
nameLabel.heightAnchor.constraint(equalToConstant: nameSize.height).isActive = true
nameLabel.widthAnchor.constraint(equalToConstant: nameSize.width).isActive = true
// nameLabel.leadingAnchor.constraint(equalTo: margins.leadingAnchor).isActive = true
self.addSubview(nameLabel)
self.addSubview(textLabel)
}
}
此代码可以正常运行(但未达到预期效果)。如果我取消注释行
// nameLabel.leadingAnchor.constraint(equalTo: margins.leadingAnchor).isActive = true
然后在标准编辑器中切换到Main.storyboard,我得到以下错误:
file:///Users/tim.daley/AixNPanes/iOS/location/location/Base.lproj/Main.storyboard:错误:IB Designables:无法呈现和更新 UIViewController 的自动布局状态(29D-8H-aV6 ): 代理抛出异常。
我是 运行 Xcode macOS Mojave 10.14.5 上的版本 10.2.1 (10E1001)
您可能需要在约束之前添加视图
self.addSubview(nameLabel)
self.addSubview(textLabel)
nameLabel.translatesAutoresizingMaskIntoConstraints = false
NSLayoutConstraint.activate([
nameLabel.heightAnchor.constraint(equalToConstant: nameSize.height),
nameLabel.widthAnchor.constraint(equalToConstant: nameSize.width),
nameLabel.leadingAnchor.constraint(equalTo: margins.leadingAnchor)
])