我有一个水平的UIStackView,默认情况下如下所示:

swift - UIStackView setCustomSpacing在运行时-LMLPHP

带有心脏的 View 最初被隐藏,然后在运行时显示。我想缩小心脏 View 和帐户名称 View 之间的间距。
以下代码可以完成此工作,但仅当在viewDidLoad中执行时才起作用:

stackView.setCustomSpacing(8, after: heartView)

稍后更改自定义间距时,例如按一下按钮,则没有任何效果。现在,这里的问题是,一旦堆栈 View 内的 subview 发生更改,自定义间隔就会丢失:当从堆栈 View 取消隐藏 View 时,自定义间隔将被重置且无法修改。

我尝试过的事情:
  • 验证间距是通过打印stackView.customSpacing(after: heartView)(正确返回8)设置的
  • 未成功运行几个重载函数:
  • stackView.layoutIfNeeded()
  • stackView.layoutSubviews()
  • view.layoutIfNeeded()
  • view.layoutSubviews()
  • viewDidLayoutSubviews()

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

    最佳答案

    您需要确保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
    

    关于swift - UIStackView setCustomSpacing在运行时,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/49306236/

    10-10 23:12