1

我正在尝试使 StackView 内容可滚动。我插入了虚拟 UIImage 视图以尝试使其工作,但它不滚动。Stack 和 Scroll 视图已经连接到 IB。我错过了什么?

import UIKit

class ViewController: UIViewController {

    @IBOutlet weak var menuStackView: UIStackView!
    @IBOutlet weak var menuScrollView: UIScrollView!
    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.


        var itemImageView = UIImageView(image: UIImage(named: "splash2"))
        itemImageView.contentMode = .ScaleAspectFit
        self.menuStackView.addArrangedSubview(itemImageView)

        itemImageView = UIImageView(image: UIImage(named: "splash2"))
        itemImageView.contentMode = .ScaleAspectFit
        self.menuStackView.addArrangedSubview(itemImageView)

        itemImageView = UIImageView(image: UIImage(named: "splash2"))
        itemImageView.contentMode = .ScaleAspectFit
        self.menuStackView.addArrangedSubview(itemImageView)

        itemImageView = UIImageView(image: UIImage(named: "splash2"))
        itemImageView.contentMode = .ScaleAspectFit
        self.menuStackView.addArrangedSubview(itemImageView)

        itemImageView = UIImageView(image: UIImage(named: "splash2"))
        itemImageView.contentMode = .ScaleAspectFit
        self.menuStackView.addArrangedSubview(itemImageView)

        itemImageView = UIImageView(image: UIImage(named: "splash2"))
        itemImageView.contentMode = .ScaleAspectFit
        self.menuStackView.addArrangedSubview(itemImageView)

        itemImageView = UIImageView(image: UIImage(named: "splash2"))
        itemImageView.contentMode = .ScaleAspectFit
        self.menuStackView.addArrangedSubview(itemImageView)


        itemImageView = UIImageView(image: UIImage(named: "splash2"))
        itemImageView.contentMode = .ScaleAspectFit
        self.menuStackView.addArrangedSubview(itemImageView)


    }

    override func viewDidLayoutSubviews() {
        super.viewDidLayoutSubviews()
        menuScrollView.contentSize = CGSize(width: menuStackView.frame.width, height: menuStackView.frame.height)

    }
}
4

1 回答 1

2

我的想法是创建一个 UIView 来设置你的滚动视图。然后将堆栈视图添加到该内容视图。因为否则,要使 stackview 像您希望的那样工作,我必须在它的所有侧面添加约束到滚动视图并添加高度和宽度约束。

我在这里也回答了一个类似的问题,可能会有所帮助:

https://stackoverflow.com/a/32999501/4626443

这是一个项目的链接,该项目具有一个工作滚动的堆栈视图,您可以使用它来重新开始:

https://github.com/Rnorback/ScrollingStackView

于 2015-10-07T18:29:27.700 回答