UIStackView setCustomSpacing 在运行时

UIStackView setCustomSpacing at runtime

我有一个水平 UIStackView,默认情况下,它看起来如下:

带有心脏的视图最初是隐藏的,然后在运行时显示。我想缩小心形视图和账户名视图之间的间距。
下面的代码完成了这项工作,但是 ,当在 viewDidLoad:

中执行时
stackView.setCustomSpacing(8, after: heartView)

稍后更改自定义间距时,比如按下按钮时,它没有任何效果。现在,这里的问题是,一旦堆栈视图中的子视图发生变化,自定义间距就会丢失:当 un-/hiding 从堆栈视图查看时,自定义间距将被重置且无法修改。

东西,我试过:

如何在运行时更新堆栈视图的自定义间距?

您需要确保 UIStackViewdistribution 属性 设置为 .fill.fillProportionally


我创建了以下 swift 游乐场,看起来我可以在运行时使用 setCustomSpacing 随机值并查看其效果。

import UIKit
import PlaygroundSupport

public class VC: UIViewController {

    let view1 = UIView()
    let view2 = UIView()
    let view3 = UIView()
    var stackView: UIStackView!

    public init() {
        super.init(nibName: nil, bundle: nil)
    }

    public required init?(coder aDecoder: NSCoder) {
        fatalError()
    }

    public override func viewDidLoad() {
        super.viewDidLoad()
        view.backgroundColor = .white
        view1.backgroundColor = .red
        view2.backgroundColor = .green
        view3.backgroundColor = .blue

        view2.isHidden = true

        stackView = UIStackView(arrangedSubviews: [view1, view2, view3])
        stackView.spacing = 10
        stackView.axis = .horizontal
        stackView.distribution = .fillProportionally

        let uiSwitch = UISwitch()
        uiSwitch.addTarget(self, action: #selector(onSwitch), for: .valueChanged)

        view1.addSubview(uiSwitch)
        uiSwitch.translatesAutoresizingMaskIntoConstraints = false

        NSLayoutConstraint.activate([
            uiSwitch.centerXAnchor.constraint(equalTo: view1.centerXAnchor),
            uiSwitch.centerYAnchor.constraint(equalTo: view1.centerYAnchor)
        ])

        view.addSubview(stackView)
        stackView.translatesAutoresizingMaskIntoConstraints = false

        NSLayoutConstraint.activate([
            stackView.heightAnchor.constraint(equalToConstant: 50),
            stackView.centerYAnchor.constraint(equalTo: view.centerYAnchor),
            stackView.leadingAnchor.constraint(equalTo: view.leadingAnchor, constant: 50),
            stackView.trailingAnchor.constraint(equalTo: view.trailingAnchor, constant: -50)
        ])
    }

    @objc public func onSwitch(sender: Any) {
        view2.isHidden = !view2.isHidden
        if !view2.isHidden {
            stackView.setCustomSpacing(CGFloat(arc4random_uniform(40)), after: view2)
        }
    }
}

PlaygroundPage.current.liveView = VC()
PlaygroundPage.current.needsIndefiniteExecution = true

另一个 setCustomSpacing 可能失败的原因是,如果您在 之前调用它 添加您想要应用间距的排列子视图。

不会工作:

headerStackView.setCustomSpacing(50, after: myLabel)
headerStackView.addArrangedSubview(myLabel)

有效:

headerStackView.addArrangedSubview(myLabel)
headerStackView.setCustomSpacing(50, after: myLabel)