1
@IBAction func addNewImage(sender: UIButton) {
    let rect = CGRectMake(0, 0, 30, 30)
    let myView = UIView(frame: rect)
    myView.backgroundColor = UIColor.greenColor()
    myView.alpha = 1.0
    myStackView.addArrangedSubview(myView)
}

This code above is just an action triggered by a button. All I want to do is just add a new UIViewto the UIStackView which is named myStackView here, and finally I found the stackView re-layout all the arrangedViews but the newly added view can't be seen, I don't know why.

Thank you for your great answers below.

4

1 回答 1

3

尝试这个,

@IBAction func buttonPressed(sender: AnyObject) {

    let rect = CGRectMake(0, 0, 30, 30)
    let myView = UIView(frame: rect)
    myView.backgroundColor = UIColor.greenColor()
    myView.alpha = 1.0
    myView.heightAnchor.constraintEqualToConstant(20).active = true
    myView.widthAnchor.constraintEqualToConstant(20).active = true
    myStackView.addArrangedSubview(myView)

}

它应该工作。

于 2015-12-28T04:31:25.477 回答