对 UILabel 列表设置约束
Setting constraints on a list of UILabels
我正在尝试通过使用锚点作为约束来创建 UILabel 的垂直列表,但出于某种原因,UILabel 未在模拟器中显示。这就是我正在做的:
let propertyNameLabel = UILabel();
propertyNameLabel.frame = CGRectMake(0,0, 100, 100);
propertyNameLabel.text = propertyName;
propertyNameLabel.textColor = UIColor.whiteColor();
propertyNameLabel.textAlignment = .Center;
self.addSubview(propertyNameLabel);
var last_anchor = propertyNameLabel.bottomAnchor;
for variable in variableList
{
let propertyNameLabel_temp = UILabel();
propertyNameLabel_temp.text = variable;
propertyNameLabel_temp.textColor = UIColor.whiteColor();
propertyNameLabel_temp.translatesAutoresizingMaskIntoConstraints = false;
self.addSubview(propertyNameLabel_temp);
propertyNameLabel_temp.topAnchor.constraintEqualToAnchor(last_anchor, constant: 10)
propertyNameLabel_temp.bottomAnchor.constraintEqualToAnchor(last_anchor, constant: 50)
propertyNameLabel_temp.widthAnchor.constraintEqualToAnchor(self.widthAnchor);
last_anchor = propertyNameLabel_temp.bottomAnchor;
}
其中 variabeList 只是一个变量列表。唯一显示的 UILabel 是我在顶部明确声明的 UILabel (propertyNameLabel)。我哪里错了?
您的代码充满 错误。您不能将 frame
用于 propertyNameLabel
并将约束用于其他标签。您的标签约束永远不会为标签提供任何类型的水平位置。
将视图视为具有四个特征:x 位置、y 位置、高度和宽度。您的约束必须明确地确定自动布局中涉及的所有视图的所有四个!否则什么也看不到。
Cocoa 提供了一些方法,您可以调用这些方法来了解您的约束是否有歧义(布局不足)。您还可以使用视图调试。使用提供的工具!请勿猜测。
下面是创建一列标签的实际代码(在超级视图中 sv
)。看看需要指定多少才能让它起作用!
var con = [NSLayoutConstraint]()
con.appendContentsOf(
NSLayoutConstraint.constraintsWithVisualFormat(
"H:|[sv]|",
options:[], metrics:nil,
views:["sv":sv]))
con.appendContentsOf(
NSLayoutConstraint.constraintsWithVisualFormat(
"V:|[sv]|",
options:[], metrics:nil,
views:["sv":sv]))
var previousLab : UILabel? = nil
for i in 0 ..< 30 {
let lab = UILabel()
// lab.backgroundColor = UIColor.redColor()
lab.translatesAutoresizingMaskIntoConstraints = false
lab.text = "This is label \(i+1)"
sv.addSubview(lab)
con.appendContentsOf(
NSLayoutConstraint.constraintsWithVisualFormat(
"H:|-(10)-[lab]",
options:[], metrics:nil,
views:["lab":lab]))
if previousLab == nil { // first one, pin to top
con.appendContentsOf(
NSLayoutConstraint.constraintsWithVisualFormat(
"V:|-(10)-[lab]",
options:[], metrics:nil,
views:["lab":lab]))
} else { // all others, pin to previous
con.appendContentsOf(
NSLayoutConstraint.constraintsWithVisualFormat(
"V:[prev]-(10)-[lab]",
options:[], metrics:nil,
views:["lab":lab, "prev":previousLab!]))
}
previousLab = lab
}
我正在尝试通过使用锚点作为约束来创建 UILabel 的垂直列表,但出于某种原因,UILabel 未在模拟器中显示。这就是我正在做的:
let propertyNameLabel = UILabel();
propertyNameLabel.frame = CGRectMake(0,0, 100, 100);
propertyNameLabel.text = propertyName;
propertyNameLabel.textColor = UIColor.whiteColor();
propertyNameLabel.textAlignment = .Center;
self.addSubview(propertyNameLabel);
var last_anchor = propertyNameLabel.bottomAnchor;
for variable in variableList
{
let propertyNameLabel_temp = UILabel();
propertyNameLabel_temp.text = variable;
propertyNameLabel_temp.textColor = UIColor.whiteColor();
propertyNameLabel_temp.translatesAutoresizingMaskIntoConstraints = false;
self.addSubview(propertyNameLabel_temp);
propertyNameLabel_temp.topAnchor.constraintEqualToAnchor(last_anchor, constant: 10)
propertyNameLabel_temp.bottomAnchor.constraintEqualToAnchor(last_anchor, constant: 50)
propertyNameLabel_temp.widthAnchor.constraintEqualToAnchor(self.widthAnchor);
last_anchor = propertyNameLabel_temp.bottomAnchor;
}
其中 variabeList 只是一个变量列表。唯一显示的 UILabel 是我在顶部明确声明的 UILabel (propertyNameLabel)。我哪里错了?
您的代码充满 错误。您不能将 frame
用于 propertyNameLabel
并将约束用于其他标签。您的标签约束永远不会为标签提供任何类型的水平位置。
将视图视为具有四个特征:x 位置、y 位置、高度和宽度。您的约束必须明确地确定自动布局中涉及的所有视图的所有四个!否则什么也看不到。
Cocoa 提供了一些方法,您可以调用这些方法来了解您的约束是否有歧义(布局不足)。您还可以使用视图调试。使用提供的工具!请勿猜测。
下面是创建一列标签的实际代码(在超级视图中 sv
)。看看需要指定多少才能让它起作用!
var con = [NSLayoutConstraint]()
con.appendContentsOf(
NSLayoutConstraint.constraintsWithVisualFormat(
"H:|[sv]|",
options:[], metrics:nil,
views:["sv":sv]))
con.appendContentsOf(
NSLayoutConstraint.constraintsWithVisualFormat(
"V:|[sv]|",
options:[], metrics:nil,
views:["sv":sv]))
var previousLab : UILabel? = nil
for i in 0 ..< 30 {
let lab = UILabel()
// lab.backgroundColor = UIColor.redColor()
lab.translatesAutoresizingMaskIntoConstraints = false
lab.text = "This is label \(i+1)"
sv.addSubview(lab)
con.appendContentsOf(
NSLayoutConstraint.constraintsWithVisualFormat(
"H:|-(10)-[lab]",
options:[], metrics:nil,
views:["lab":lab]))
if previousLab == nil { // first one, pin to top
con.appendContentsOf(
NSLayoutConstraint.constraintsWithVisualFormat(
"V:|-(10)-[lab]",
options:[], metrics:nil,
views:["lab":lab]))
} else { // all others, pin to previous
con.appendContentsOf(
NSLayoutConstraint.constraintsWithVisualFormat(
"V:[prev]-(10)-[lab]",
options:[], metrics:nil,
views:["lab":lab, "prev":previousLab!]))
}
previousLab = lab
}