UIScrollView项目不会从后台移动

时间:2018-10-18 01:09:58

标签: swift uiscrollview autolayout swift4 snapkit

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

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

1 个答案:

答案 0 :(得分:1)

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

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

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

每次 是指您创建的{strong> 另一个 实例scrollView 。运行您的代码,并使用Debug View层次结构,这是您得到的:

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

我在代码中包含的注释应阐明这样做的方式和原因。