我有一个像这样的可设计视图(称为 ReviewItem )。它的工作非常完美。
override init(frame: CGRect) {
super.init(frame: frame)
setupUI()
}
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
setupUI()
}
override func layoutSubviews() {
super.layoutSubviews()
let contentFrame = bounds
let imagePositionX = (contentFrame.width - imageSize.width) / 2
normalImageView.frame = CGRect(origin: CGPoint(x: imagePositionX, y: padding), size: imageSize)
let labelPositionY = imageSize.height + padding*2
textLabel.frame = CGRect(x: 0, y: labelPositionY, width: contentFrame.width, height: contentFrame.height - labelPositionY)
}
func setupUI() {
let _normalImageView = UIImageView()
addSubview(_normalImageView)
normalImageView = _normalImageView
let _textLabel = UILabel()
addSubview(_textLabel)
textLabel = _textLabel
}
但是现在我想创建一个新的 ReviewBar ,它包含许多 ReviewItem 元素。代码如下:
override func layoutSubviews() {
super.layoutSubviews()
let contentFrame = bounds
let itemWidth = contentFrame.width / CGFloat(itemsNumber)
for i in 0..<itemsNumber {
let frame = CGRect(x: itemWidth*CGFloat(i), y: 0, width: itemWidth, height: contentFrame.height)
let item = reviewItems[i]
item.frame = frame
}
}
func setupUI() {
removeAllItems() //remove all subviews
reviewItems.removeAll()
for i in 0..<itemsNumber {
let item = ReviewBarItem(frame: .zero)
addSubview(item)
reviewItems.append(item)
}
}
ReviewBar 未在情节提要上显示并出现错误。但是,当我为它的属性设置值并在模拟器上运行时,它会根据需要显示。我想 ReviewBar 可以在故事板上显示,我该怎么做?感谢
IB Designables: Failed to render and update auto layout status for CustomViewDemoVC (4Tk-Ic-u7T): The agent crashed