以编程方式使用约束设置UIScrollView的正确方法?

时间:2017-10-01 15:36:06

标签: ios swift uiscrollview

我正在尝试使用约束以编程方式并排放置的两个视图控制器之间创建一个简单的水平滚动。我在这里看到的工作似乎有效,但我读过的所有内容都表明,如果约束设置正确,我不需要配置contentSize的大小 - autolayout会为我做这个。但是,当我从scrollView.contentSize = CGSize(width: view.bounds.width * 2, height: view.bounds.height)中删除viewWillLayoutSubviews时,它不会滚动。我哪里出错了?

class ViewController0: UIViewController {


    let scrollView = UIScrollView()
    let contentView = UIView()
    let page1 = ViewController1()
    let page2 = ViewController2()


    override func loadView() {

        setView()
        addScrollView()
        fillScrollView()

    }


    override func viewDidLoad() {
        super.viewDidLoad()
    }


    override func viewWillLayoutSubviews() {
        super.viewWillLayoutSubviews()

        scrollView.contentSize = CGSize(width: view.bounds.width * 2, height: view.bounds.height)

    }


    func setView() {

        view = UIView()
        view.frame = UIScreen.main.bounds
        view.backgroundColor = UIColor.blue

    }


    func addScrollView() {

        scrollView.bounces = false
        scrollView.isPagingEnabled = true
        scrollView.backgroundColor = UIColor.brown
        scrollView.translatesAutoresizingMaskIntoConstraints = false
        view.addSubview(scrollView)

        scrollView.leadingAnchor.constraint(equalTo: view.leadingAnchor).isActive = true
        scrollView.trailingAnchor.constraint(equalTo: view.trailingAnchor).isActive = true
        scrollView.topAnchor.constraint(equalTo: view.topAnchor).isActive = true
        scrollView.bottomAnchor.constraint(equalTo: view.bottomAnchor).isActive = true

        contentView.backgroundColor = UIColor.green
        contentView.translatesAutoresizingMaskIntoConstraints = false
        scrollView.addSubview(contentView)

        contentView.leadingAnchor.constraint(equalTo: scrollView.leadingAnchor).isActive = true
        contentView.topAnchor.constraint(equalTo: scrollView.topAnchor).isActive = true
        contentView.heightAnchor.constraint(equalTo: view.heightAnchor, multiplier: 1).isActive = true
        contentView.widthAnchor.constraint(equalTo: view.widthAnchor, multiplier: 2).isActive = true

    }


    func fillScrollView() {

        addChildViewController(page1)
        page1.didMove(toParentViewController: self)
        page1.view.translatesAutoresizingMaskIntoConstraints = false
        contentView.addSubview(page1.view)
        page1.view.leadingAnchor.constraint(equalTo: contentView.leadingAnchor).isActive = true
        page1.view.topAnchor.constraint(equalTo: contentView.topAnchor).isActive = true
        page1.view.bottomAnchor.constraint(equalTo: contentView.bottomAnchor).isActive = true
        page1.view.widthAnchor.constraint(equalTo: view.widthAnchor, multiplier: 1).isActive = true

        addChildViewController(page2)
        page2.didMove(toParentViewController: self)
        page2.view.translatesAutoresizingMaskIntoConstraints = false
        contentView.addSubview(page2.view)
        page2.view.leadingAnchor.constraint(equalTo: page1.view.trailingAnchor).isActive = true
        page2.view.topAnchor.constraint(equalTo: contentView.topAnchor).isActive = true
        page2.view.bottomAnchor.constraint(equalTo: contentView.bottomAnchor).isActive = true
        page2.view.widthAnchor.constraint(equalTo: view.widthAnchor, multiplier: 1).isActive = true

    }


}

1 个答案:

答案 0 :(得分:1)

这应该有效

    class ViewController0: UIViewController {

    let scrollView = UIScrollView()
    let page1 = ViewController1()
    let page2 = ViewController1()

    override func loadView() {
        setView()
        addScrollView()
        setupPage()
    }

    func setView() {

        view = UIView()
        view.frame = UIScreen.main.bounds
        view.backgroundColor = UIColor.blue

    }

    func addScrollView() {

        scrollView.backgroundColor = UIColor.brown
        scrollView.translatesAutoresizingMaskIntoConstraints = false
        view.addSubview(scrollView)
        scrollView.leadingAnchor.constraint(equalTo: view.leadingAnchor).isActive = true
        scrollView.trailingAnchor.constraint(equalTo: view.trailingAnchor).isActive = true
        scrollView.topAnchor.constraint(equalTo: view.topAnchor).isActive = true
        scrollView.bottomAnchor.constraint(equalTo: view.bottomAnchor).isActive = true
    }

    func setupPage() {
        page1.view.translatesAutoresizingMaskIntoConstraints = false
        scrollView.addSubview(page1.view)
        addChildViewController(page1)
        page1.didMove(toParentViewController: self)

        page2.view.translatesAutoresizingMaskIntoConstraints = false
        scrollView.addSubview(page2.view)
        addChildViewController(page2)
        page2.didMove(toParentViewController: self)

        let views: [String: UIView] = ["view": view, "page1": page1.view, "page2": page2.view]
        let verticalConstraints = NSLayoutConstraint.constraints(withVisualFormat: "V:|[page1(==view)]|", options: [], metrics: nil, views: views)
        let horizontalConstraints = NSLayoutConstraint.constraints(withVisualFormat: "H:|[page1(==view)][page2(==view)]|", options: [.alignAllTop, .alignAllBottom], metrics: nil, views: views)

        NSLayoutConstraint.activate(verticalConstraints + horizontalConstraints)
    }

}