UIScrollView项目不从后台移动

问题描述 投票:0回答:1

我设置了约束,以便在加载时我会有一个占用屏幕的图表,但是您可以向下滚动以查看更多信息。但是,我可以看到滚动条移动,图表不动!

var scrollView: UIScrollView {
    let scroll = UIScrollView()
    self.view.addSubview(scroll)
    scroll.snp.makeConstraints { (make) in
        make.edges.equalTo(self.view)
    }
    return scroll

}
var contentView: UIView {
    let content = UIView()
    self.scrollView.addSubview(content)
    content.snp.makeConstraints { (make) in
        make.top.bottom.equalTo(self.scrollView)
        make.left.right.equalTo(self.view)
        make.width.equalTo(self.scrollView)
    }
    // self.scrollView.contentSize = content.frame.size
    return content
}
override func viewDidLoad() {
    super.viewDidLoad()
    self.contentView.addSubview(self.chart)

    self.chart.snp.makeConstraints { (make) in
        // http://snapkit.io/docs/
        // https://github.com/SnapKit/SnapKit/issues/448
        make.top.equalTo(self.contentView)
        make.left.right.equalTo(self.contentView)
        make.height.equalTo(self.view).offset(-(self.tabBarController?.tabBar.frame.height)!)
    }
    self.scrollView.contentSize = CGSize(width: self.view.frame.width, height: 3000) // Testing

}

滚动条会移动,但内容不会移动。 enter image description here

swift uiscrollview autolayout swift4 snapkit
1个回答
1
投票

不幸的是,你做了很多错事。

首先,当您使用此构造时:

var scrollView: UIScrollView {
    let scroll = UIScrollView()
    self.view.addSubview(scroll)
    scroll.snp.makeConstraints { (make) in
        make.edges.equalTo(self.view)
    }
    return scroll
}

每次引用scrollView时,您都会创建另一个滚动视图实例。运行代码,并使用Debug View Hierarchy,这就是你得到的:

enter image description here

red视图是scrollViews,blue视图是contentViews。如你所见,这绝对不是你想要的。

以下是您可以执行以下操作的示例:

class ViewController: UIViewController {

    var scrollView: UIScrollView = {
        let scroll = UIScrollView()
        scroll.backgroundColor = .red
        return scroll

    }()

    var contentView: UIView  = {
        let content = UIView()
        content.backgroundColor = .blue
        return content
    }()

    var chart: UILabel = {
        let v = UILabel()
        v.numberOfLines = 0
        v.text = "This is the\nChart View"
        v.backgroundColor = .cyan
        v.textAlignment = .center
        return v
    }()

    var bottomLabel: UILabel = {
        let v = UILabel()
        v.text = "The Bottom Label"
        v.backgroundColor = .yellow
        v.textAlignment = .center
        return v
    }()

    override func viewDidLoad() {
        super.viewDidLoad()

        // add scrollView
        self.view.addSubview(scrollView)

        // constrain to safe area (so it doesn't extend below the tab bar)
        scrollView.snp.makeConstraints { (make) in
            make.edges.equalTo(self.view.safeAreaLayoutGuide)
        }

        // add contentView to scrollView
        self.scrollView.addSubview(contentView)

        // constrain all 4 edges to scrollView
        // this will allow auto-layout to control / define the .contentSize
        contentView.snp.makeConstraints { (make) in
            make.edges.equalTo(self.scrollView)
        }

        // constrain contentView's width to scrollView's width
        // but we *don't* constrain its height
        contentView.snp.makeConstraints { (make) in
            make.width.equalTo(self.scrollView)
        }

        // add the chart to contentView
        self.contentView.addSubview(self.chart)

        // constrain chart to top, left and right of contentView
        // constrain its height to scrollView's height (so it initially fills the visible area
        chart.snp.makeConstraints { (make) in
            make.top.equalTo(self.contentView)
            make.left.right.equalTo(self.contentView)
            make.height.equalTo(self.scrollView)
        }

        // now, we'll add a label to scrollView
        self.contentView.addSubview(self.bottomLabel)

        // constrain the label left and right with 20-pts, and a height of 40
        bottomLabel.snp.makeConstraints { (make) in
            make.left.equalTo(self.contentView).offset(20)
            make.right.equalTo(self.contentView).offset(-20)
            make.height.equalTo(40)
        }

        // constrain the label's TOP 20-pts below the BOTTOM of chart view
        bottomLabel.snp.makeConstraints { (make) in
            make.top.equalTo(self.chart.snp.bottom).offset(20)
        }

        // constrain the BOTTOM of the label 20-pts from the bottom of contentView
        bottomLabel.snp.makeConstraints { (make) in
            make.bottom.equalTo(self.contentView).offset(-20)
        }

        // with those constraints, contentView's height is now:
        // chart's height + 20 + label's height + 20
        // and because contentView's bottom is constrained to scrollView's bottom,
        // that becomes scrollView's contentSize

    }

}

我添加了一个UILabel作为chart视图,并在其下面添加了另一个标签,以显示如何使用自动布局来定义滚动内容。

初步观点:

enter image description here

向上滚动:

enter image description here

和,生成的视图层次结构:

enter image description here

我在代码中包含的注释应该澄清它是如何以及为什么以这种方式完成的。

© www.soinside.com 2019 - 2024. All rights reserved.