为什么堆栈视图的框架是(0.0,0.0)?
why the stack view's frame is (0.0, 0.0)?
我正在尝试自动布局我的堆栈视图并在代码中创建其 arrangedSubView(2 个标签和 1 个图像),但模拟器中只出现一组堆栈视图。当在控制台中打印结果时,所有标签和图像都有值,除了所有堆栈视图的框架是 0.0;0.0
。为什么会这样?为什么堆栈视图不出现?请在下面查看我的代码:
code
let contentView: UIView = UIView()
let stackView: UIStackView = UIStackView()
var singleStack = Array(count: 6, repeatedValue: UIStackView())
var timeLabel = Array(count: 6, repeatedValue: UILabel())
var iconImage = Array(count: 6, repeatedValue: UIImageView())
var tempLabel = Array(count: 6, repeatedValue: UILabel())
override func viewDidLoad() {
super.viewDidLoad()
for index in 0...5 {
timeLabel[index].text = "0\(index):00"
iconImage[index].image = UIImage(named: "sunny")!
tempLabel[index].text = "0\(index)°"
singleStack[index].addArrangedSubview(timeLabel[index])
singleStack[index].addArrangedSubview(iconImage[index])
singleStack[index].addArrangedSubview(tempLabel[index])
singleStack[index].translatesAutoresizingMaskIntoConstraints = false
singleStack[index].axis = .Vertical
singleStack[index].alignment = .Center
singleStack[index].distribution = .FillEqually
stackView.addArrangedSubview(singleStack[index])
print("\(timeLabel[index].text!)")
print("\(iconImage[index].image!)")
print("\(tempLabel[index].text!)")
print("\(singleStack[index])")
print("\(stackView)")
}
stackView.translatesAutoresizingMaskIntoConstraints = false
stackView.axis = .Horizontal
stackView.alignment = .Fill
stackView.distribution = .FillEqually
contentView.backgroundColor = UIColor.lightGrayColor()
view.addSubview(contentView)
contentView.addSubview(stackView)
contentView.translatesAutoresizingMaskIntoConstraints = false
NSLayoutConstraint.activateConstraints(NSLayoutConstraint.constraintsWithVisualFormat(
"H:|[stackView]|",
options: [],
metrics: nil,
views: ["stackView": stackView]))
NSLayoutConstraint.activateConstraints(NSLayoutConstraint.constraintsWithVisualFormat(
"V:|[stackView]|",
options: [],
metrics: nil,
views: ["stackView": stackView]))
NSLayoutConstraint.activateConstraints(NSLayoutConstraint.constraintsWithVisualFormat(
"H:|-[contentView]-|",
options: [],
metrics: nil,
views: ["contentView": contentView]))
NSLayoutConstraint.activateConstraints(NSLayoutConstraint.constraintsWithVisualFormat(
"V:[contentView(120)]-16-|",
options: [],
metrics: nil,
views: ["contentView": contentView]))
}
The printed results in console (just use one piece for example)
01:00
<UIImage: 0x7fb61944f630>, {32, 32}
01°
<UIStackView: 0x7fb6194462c0; frame = (0 0; 0 0); layer = <CATransformLayer: 0x7fb619445e70>>
<UIStackView: 0x7fb619443b50; frame = (0 0; 0 0); layer = <CATransformLayer: 0x7fb6194433b0>>
如您所见,所有标签和图像都有值,但所有堆栈视图的框架都是 0.0;0.0
。
希望有人能解决我的问题,先谢谢了!
当您使用 Array(count: , repeatedValue: )
初始化一个数组并且 repeatedValue
是一个 class 时,您将获得一个指向 [=23= 的相同实例的指针数组】 数遍。 类 in swift 通过引用传递。所以 singleStack[0] === singleStack[2]
which === singleStack[5]
等等。这就是为什么只显示一个堆栈视图的原因。您应该将 for 循环更改为:
for index in 0...5 {
let singleStack = UIStackView()
let timeLabel = UILabel()
let iconImage = UIImageView()
let tempLabel = UILabel()
timeLabel.text = "0\(index):00"
iconImage.image = UIImage(named: "sunny")!
tempLabel.text = "0\(index)°"
singleStack.addArrangedSubview(timeLabel)
singleStack.addArrangedSubview(iconImage)
singleStack.addArrangedSubview(tempLabel)
singleStack.axis = .Vertical
singleStack.alignment = .Center
singleStack.distribution = .FillEqually
self.stackView.addArrangedSubview(singleStack)
}
除非你绝对需要你添加到堆栈视图的视图数组,否则使用 arrangedSubviews
会很笨拙。在这种情况下,最好使用像这样的初始化块:
let singleStack: [UIStackView] =
{
var singleStacks = [UIStackView]()
for _ in 0...5
{
singleStacks.append(UIStackView())
}
return singleStacks
}()
等等……
我正在尝试自动布局我的堆栈视图并在代码中创建其 arrangedSubView(2 个标签和 1 个图像),但模拟器中只出现一组堆栈视图。当在控制台中打印结果时,所有标签和图像都有值,除了所有堆栈视图的框架是 0.0;0.0
。为什么会这样?为什么堆栈视图不出现?请在下面查看我的代码:
code
let contentView: UIView = UIView()
let stackView: UIStackView = UIStackView()
var singleStack = Array(count: 6, repeatedValue: UIStackView())
var timeLabel = Array(count: 6, repeatedValue: UILabel())
var iconImage = Array(count: 6, repeatedValue: UIImageView())
var tempLabel = Array(count: 6, repeatedValue: UILabel())
override func viewDidLoad() {
super.viewDidLoad()
for index in 0...5 {
timeLabel[index].text = "0\(index):00"
iconImage[index].image = UIImage(named: "sunny")!
tempLabel[index].text = "0\(index)°"
singleStack[index].addArrangedSubview(timeLabel[index])
singleStack[index].addArrangedSubview(iconImage[index])
singleStack[index].addArrangedSubview(tempLabel[index])
singleStack[index].translatesAutoresizingMaskIntoConstraints = false
singleStack[index].axis = .Vertical
singleStack[index].alignment = .Center
singleStack[index].distribution = .FillEqually
stackView.addArrangedSubview(singleStack[index])
print("\(timeLabel[index].text!)")
print("\(iconImage[index].image!)")
print("\(tempLabel[index].text!)")
print("\(singleStack[index])")
print("\(stackView)")
}
stackView.translatesAutoresizingMaskIntoConstraints = false
stackView.axis = .Horizontal
stackView.alignment = .Fill
stackView.distribution = .FillEqually
contentView.backgroundColor = UIColor.lightGrayColor()
view.addSubview(contentView)
contentView.addSubview(stackView)
contentView.translatesAutoresizingMaskIntoConstraints = false
NSLayoutConstraint.activateConstraints(NSLayoutConstraint.constraintsWithVisualFormat(
"H:|[stackView]|",
options: [],
metrics: nil,
views: ["stackView": stackView]))
NSLayoutConstraint.activateConstraints(NSLayoutConstraint.constraintsWithVisualFormat(
"V:|[stackView]|",
options: [],
metrics: nil,
views: ["stackView": stackView]))
NSLayoutConstraint.activateConstraints(NSLayoutConstraint.constraintsWithVisualFormat(
"H:|-[contentView]-|",
options: [],
metrics: nil,
views: ["contentView": contentView]))
NSLayoutConstraint.activateConstraints(NSLayoutConstraint.constraintsWithVisualFormat(
"V:[contentView(120)]-16-|",
options: [],
metrics: nil,
views: ["contentView": contentView]))
}
The printed results in console (just use one piece for example)
01:00
<UIImage: 0x7fb61944f630>, {32, 32}
01°
<UIStackView: 0x7fb6194462c0; frame = (0 0; 0 0); layer = <CATransformLayer: 0x7fb619445e70>>
<UIStackView: 0x7fb619443b50; frame = (0 0; 0 0); layer = <CATransformLayer: 0x7fb6194433b0>>
如您所见,所有标签和图像都有值,但所有堆栈视图的框架都是 0.0;0.0
。
希望有人能解决我的问题,先谢谢了!
当您使用 Array(count: , repeatedValue: )
初始化一个数组并且 repeatedValue
是一个 class 时,您将获得一个指向 [=23= 的相同实例的指针数组】 数遍。 类 in swift 通过引用传递。所以 singleStack[0] === singleStack[2]
which === singleStack[5]
等等。这就是为什么只显示一个堆栈视图的原因。您应该将 for 循环更改为:
for index in 0...5 {
let singleStack = UIStackView()
let timeLabel = UILabel()
let iconImage = UIImageView()
let tempLabel = UILabel()
timeLabel.text = "0\(index):00"
iconImage.image = UIImage(named: "sunny")!
tempLabel.text = "0\(index)°"
singleStack.addArrangedSubview(timeLabel)
singleStack.addArrangedSubview(iconImage)
singleStack.addArrangedSubview(tempLabel)
singleStack.axis = .Vertical
singleStack.alignment = .Center
singleStack.distribution = .FillEqually
self.stackView.addArrangedSubview(singleStack)
}
除非你绝对需要你添加到堆栈视图的视图数组,否则使用 arrangedSubviews
会很笨拙。在这种情况下,最好使用像这样的初始化块:
let singleStack: [UIStackView] =
{
var singleStacks = [UIStackView]()
for _ in 0...5
{
singleStacks.append(UIStackView())
}
return singleStacks
}()
等等……