具有垂直stackview的UIScrollView水平滚动但不垂直滚动

时间:2020-07-26 18:25:56

标签: swift xcode uiscrollview uistackview

我遵循了类似的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)
    `

1 个答案:

答案 0 :(得分:1)

您做错了很多事情...

您的代码在 [ { 'FileID': '001', 'Match': 'AAPL', 'Freq': 3, 'MatchInfo': [ { 'CatIdType': 'food', 'CatId': '123', 'MatchCat': 'fruit' }, { 'CatIdType': 'tech', 'CatId': '456', 'MatchCat': 'company' } ] } ] 堆栈视图和@IBOutlet中都显示history,这意味着您已将它们添加到Storyboard中了吗?如果是这样,您应该 这样做:

scrollView

因为添加到情节提要中时它们已经存在。另外,希望您已在Storyboard中添加了约束。

但是,如果您想通过代码完成所有操作,请传递以下 极其简单 示例:

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

作为旁注:阅读文档并完成几个滚动视图教程将对您大有裨益。看来您尝试使用链接到的问题中的片段而不知道它的含义。