【问题标题】:UIScrollView with vertical stackview scrolls horizontally but not vertically带有垂直stackview的UIScrollView水平滚动但不垂直滚动
【发布时间】:2020-07-26 18:25:56
【问题描述】:

我遵循了类似question's 答案的确切代码,虽然它为他们垂直滚动,但对我来说却不是。代码中的标签只是一个测试,实际程序中还有更多的标签,但它们是稍后从 firebase 添加的,所以我不确定这是否会改变任何东西。虽然这不是超级重要,但我更愿意以编程方式解决这个问题,因为我在该领域更有能力。我不擅长提问或提供正确的代码,所以整个项目是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)
    `

【问题讨论】:

    标签: swift xcode uiscrollview uistackview


    【解决方案1】:

    你做错了一堆事情......

    对于history 堆栈视图和scrollView,您的代码显示@IBOutlet,这意味着您已将它们添加到情节提要中?如果是这样,您应该这样做:

    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)
            }
    
        }
        
    }
    

    附带说明:阅读文档并完成几个滚动视图教程会让您受益匪浅。您似乎试图使用您链接到的问题中的片段,却不知道其中的任何含义。

    【讨论】:

    • 这正是我所做的,非常感谢。作为一个快速的旁注,你指出的一些错误只是我绝望的“修复”,希望我能幸运。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2010-10-18
    • 2021-11-13
    • 2012-09-23
    • 2013-12-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多