具有垂直堆栈视图的 UIScrollView 水平滚动但不是垂直滚动

UIScrollView with vertical stackview scrolls horizontally but not vertically

我遵循了类似 answer and while it scrolled vertically for them it does not for me. The label in the code is just a test there are far more in the actual program but they are added from firebase later on so I'm not sure if that changes anything. While it's not super important I would prefer to figure this out programmatically as I am more capable in that area. I'm not great at asking questions or providing the right code so the whole project is here

的确切代码

`

@IBOutlet weak var history: UIStackView!
@IBOutlet weak var scrollView: UIScrollView!

var ref: DatabaseReference!


override func viewDidLoad() {
    super.viewDidLoad()
    view.addSubview(scrollView)

    ref = Database.database().reference()
    
    let label = UILabel(frame: CGRect.init())
    label.text = "Label"
    history.addArrangedSubview(label)
    
    scrollView.contentSize = CGSize(width: view.bounds.width, height: view.bounds.height)

    history.leadingAnchor.constraint(equalTo: scrollView.leadingAnchor).isActive = true
    history.trailingAnchor.constraint(equalTo: scrollView.trailingAnchor).isActive = true
    history.bottomAnchor.constraint(equalTo: scrollView.bottomAnchor).isActive = true
    history.topAnchor.constraint(equalTo: scrollView.topAnchor).isActive = true
    history.widthAnchor.constraint(equalTo: scrollView.widthAnchor).isActive = true
    //history.heightAnchor.constraint(lessThanOrEqualTo: scrollView.heightAnchor).isActive = true

    scrollView.addSubview(history)
    view.addSubview(scrollView)
    `

你做错了很多事...

您的代码为 history 堆栈视图和 scrollView 显示了 @IBOutlet,这意味着您已将它们添加到 Storyboard 中?如果是这样,你应该而不是做:

scrollView.addSubview(history)
view.addSubview(scrollView)

因为它们在添加到 Storyboard 时已经存在。此外,人们会希望您在 Storyboard 中添加约束。

但是,如果您想通过代码完成所有操作,请使用这个非常简单示例:

class ViewController: UIViewController {

    var history: UIStackView!
    var scrollView: UIScrollView!
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        history = UIStackView()
        // vertical stack
        history.axis = .vertical
        // arranged subviews fill the width
        history.alignment = .fill
        // distribution
        history.distribution = .fill
        // spacing
        history.spacing = 12
        
        scrollView = UIScrollView()
        // so we can see it
        scrollView.backgroundColor = .cyan
        
        // we're using auto-layout constraints
        scrollView.translatesAutoresizingMaskIntoConstraints = false
        history.translatesAutoresizingMaskIntoConstraints = false

        // add the stack view to the scroll view
        scrollView.addSubview(history)
        
        // add the scroll view to the view
        view.addSubview(scrollView)

        // no no no... let auto-layout handle it
        //scrollView.contentSize = CGSize(width: view.bounds.width, height: view.bounds.height)
        
        // respect safe area
        let g = view.safeAreaLayoutGuide
        
        NSLayoutConstraint.activate([
            
            // constrain scroll view with 20-pts on each side
            scrollView.topAnchor.constraint(equalTo: g.topAnchor, constant: 20.0),
            scrollView.leadingAnchor.constraint(equalTo: g.leadingAnchor, constant: 20.0),
            scrollView.trailingAnchor.constraint(equalTo: g.trailingAnchor, constant: -20.0),
            scrollView.bottomAnchor.constraint(equalTo: g.bottomAnchor, constant: -20.0),

            // constrain stack view to all 4 sides of scroll view with 8-pts on each side
            history.topAnchor.constraint(equalTo: scrollView.topAnchor, constant: 8.0),
            history.leadingAnchor.constraint(equalTo: scrollView.leadingAnchor, constant: 8.0),
            history.trailingAnchor.constraint(equalTo: scrollView.trailingAnchor, constant: -8.0),
            history.bottomAnchor.constraint(equalTo: scrollView.bottomAnchor, constant: -8.0),
            
            // constrain stack view width to scroll view width minus 16 (for 8-pts on each side)
            history.widthAnchor.constraint(equalTo: scrollView.widthAnchor, constant: -16),

        ])

        // let's add 30 labels to the stack view
        for i in 1...30 {
            let label = UILabel()
            label.text = "Label: \(i)"
            // so we can see the label frames
            label.backgroundColor = .yellow
            history.addArrangedSubview(label)
        }

    }
    
}

附带说明:阅读文档并完成 几个 滚动视图教程将使您受益匪浅。看起来你试图使用你链接到的问题中的片段,但不知道它是什么意思。