gpt4 book ai didi

ios - addArrangedSubview 并调整 StackView 的大小

转载 作者:可可西里 更新时间:2023-11-01 00:50:59 25 4
gpt4 key购买 nike

我正在尝试使用按钮将 View 添加到 StackView:

import UIKit

class ViewController: UIViewController {

@IBOutlet weak var stackOutlet: UIStackView!
@IBOutlet weak var buttonOutlet: UIButton!

override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
}

override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}

@IBAction func buttonAction(_ sender: AnyObject) {
let newView = UIView(frame: CGRect(x: 0, y: 0, width: 240, height: 128))
newView.heightAnchor.constraint(equalToConstant: 128)
newView.widthAnchor.constraint(equalToConstant: 240)
newView.backgroundColor = UIColor.green

stackOutlet.addArrangedSubview(newView)
stackOutlet.heightAnchor.constraint(equalToConstant: stackOutlet.frame.size.height + 128)
print(stackOutlet.subviews.count)
}
}

subview 的数量在增加,但屏幕上没有任何变化。我怀疑我的 StackView 没有正确调整大小,对吗?

最佳答案

没关系,我找到了解决方案:

newView.widthAnchor.constraint(equalToConstant: 240).isActive = true
newView.heightAnchor.constraint(equalToConstant: 128).isActive = true

你需要 .isActive = true

关于ios - addArrangedSubview 并调整 StackView 的大小,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39151280/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com