界面生成器不会拉伸自定义视图的子视图
Interface builder doesn't stretch subviews of custom view
我创建了带有一个内部 UILabel 的自定义视图 (FAQItemView),它被限制在超级视图的四个侧面。这是此视图的源代码:
import UIKit
@IBDesignable class FAQItemView: UIView {
var questionLabel: UILabel = UILabel()
override init(frame: CGRect) {
super.init(frame: frame)
setup()
}
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
setup()
}
func setup() {
translatesAutoresizingMaskIntoConstraints = false
addSubview(questionLabel)
questionLabel.translatesAutoresizingMaskIntoConstraints = false
questionLabel.textColor = UIColor.black
questionLabel.textAlignment = .center
questionLabel.numberOfLines = 0
questionLabel.topAnchor.constraint(equalTo: self.topAnchor).isActive = true
questionLabel.leadingAnchor.constraint(equalTo: self.leadingAnchor).isActive = true
questionLabel.trailingAnchor.constraint(equalTo: self.trailingAnchor).isActive = true
questionLabel.bottomAnchor.constraint(equalTo: self.bottomAnchor).isActive = true
questionLabel.text = "question"
questionLabel.backgroundColor = UIColor.green
}
}
我在 Interface Builder 中添加了 FAQItemView 并将其宽度限制为 200px。在这种情况下,FAQItemView 的内部标签应该拉伸到 FAQItemView 的大小。当我 运行 应用程序时一切正常,但在 Interface Builder 中标签位于容器的左侧,其默认(固有)大小。
示例项目可在 https://www.dropbox.com/s/u2923l8exqtg3ir/testapp1.zip?dl=0 获得。这里的 FAQItemView 有红色背景,内部标签有绿色背景。在 运行 时间里,红色不可见,因为标签有绿色背景,但在 Interface Builder 中,红色也可见(在绿色背景标签的右侧)
有人能说我做错了什么吗?
提前致谢。
UPD: Screenshot of view in interface builder
糟糕...
不要在您的自定义视图本身上设置translatesAutoresizingMaskIntoConstraints = false
。
所以,只需删除 setup()
中的第一行:
func setup() {
//translatesAutoresizingMaskIntoConstraints = false
这应该可以解决问题。
我创建了带有一个内部 UILabel 的自定义视图 (FAQItemView),它被限制在超级视图的四个侧面。这是此视图的源代码:
import UIKit
@IBDesignable class FAQItemView: UIView {
var questionLabel: UILabel = UILabel()
override init(frame: CGRect) {
super.init(frame: frame)
setup()
}
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
setup()
}
func setup() {
translatesAutoresizingMaskIntoConstraints = false
addSubview(questionLabel)
questionLabel.translatesAutoresizingMaskIntoConstraints = false
questionLabel.textColor = UIColor.black
questionLabel.textAlignment = .center
questionLabel.numberOfLines = 0
questionLabel.topAnchor.constraint(equalTo: self.topAnchor).isActive = true
questionLabel.leadingAnchor.constraint(equalTo: self.leadingAnchor).isActive = true
questionLabel.trailingAnchor.constraint(equalTo: self.trailingAnchor).isActive = true
questionLabel.bottomAnchor.constraint(equalTo: self.bottomAnchor).isActive = true
questionLabel.text = "question"
questionLabel.backgroundColor = UIColor.green
}
}
我在 Interface Builder 中添加了 FAQItemView 并将其宽度限制为 200px。在这种情况下,FAQItemView 的内部标签应该拉伸到 FAQItemView 的大小。当我 运行 应用程序时一切正常,但在 Interface Builder 中标签位于容器的左侧,其默认(固有)大小。
示例项目可在 https://www.dropbox.com/s/u2923l8exqtg3ir/testapp1.zip?dl=0 获得。这里的 FAQItemView 有红色背景,内部标签有绿色背景。在 运行 时间里,红色不可见,因为标签有绿色背景,但在 Interface Builder 中,红色也可见(在绿色背景标签的右侧)
有人能说我做错了什么吗?
提前致谢。
UPD: Screenshot of view in interface builder
糟糕...
不要在您的自定义视图本身上设置translatesAutoresizingMaskIntoConstraints = false
。
所以,只需删除 setup()
中的第一行:
func setup() {
//translatesAutoresizingMaskIntoConstraints = false
这应该可以解决问题。