为什么堆栈视图的帧是(0.0,0.0)?

时间:2016-08-28 23:35:29

标签: ios swift uistackview

我正在尝试自动布局我的堆栈视图并在代码中创建arrangeSubView(2个标签和1个图像),但在模拟器中只出现一组堆栈视图。在控制台中打印结果时,除了所有堆栈视图的帧都是0.0;0.0之外,所有标签和图像都有值。为什么会这样?为什么堆栈视图不会出现?请参阅下面的代码:

  

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]))

}
  

打印结果在控制台中(例如只使用一件)

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

希望任何人都能解决我的问题,非常感谢你!

1 个答案:

答案 0 :(得分:1)

使用Array(count: , repeatedValue: )初始化数组并且repeatedValue是一个类时,您将获得指向同一个类计数次数的指针数组。 swift中的类通过引用传递。所以singleStack[0] === singleStack[2] === 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
}()

等...