如何在iOS中使用带有Swift的scrollView进行自动布局?

时间:2016-12-24 01:51:09

标签: ios swift uiscrollview scrollview

我尝试将视图添加到我的scrollView,当我手动将宽度设置为greenView它工作正常,但如果我尝试使用约束,似乎约束不起作用。我有什么不对吗?有人可以帮忙吗?

这是我的控制器和视图代码:

import UIKit
import Material

class UserProfileViewController: UIViewController {

    override func loadView(){
        self.view = UserProfileView()
    }

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

}


import UIKit
import Material

class UserProfileView: UIView {

    fileprivate var scrollView: UIScrollView!

    convenience init(){
        self.init(frame: CGRect.zero)
        self.backgroundColor = UIColor(red:0.15, green:0.24, blue:0.37, alpha:1.0)
        prepareScrollView()
        prepareMainView()
    }

    fileprivate func prepareScrollView() {
        self.scrollView = UIScrollView(frame: UIScreen.main.bounds)
        self.scrollView.contentSize = CGSize(width:self.scrollView.width, height: 1678)
        self.addSubview(scrollView)
    }

    fileprivate func prepareMainView() {
        let greenView = UIView()
        greenView.backgroundColor = UIColor.green
        greenView.height = 100


        self.scrollView.addSubview(greenView)

        self.scrollView.translatesAutoresizingMaskIntoConstraints = false
        self.scrollView.isScrollEnabled = true
        self.scrollView.isPagingEnabled = true
        greenView.translatesAutoresizingMaskIntoConstraints = false

        let views = [
            "scrollView": self.scrollView!,
            "greenView": greenView,
        ] as [String : Any]



        let scrollViewHeight = NSLayoutConstraint.constraints(withVisualFormat: "V:|[scrollView]|", metrics: nil, views: views)
        let scrollViewWidth = NSLayoutConstraint.constraints(withVisualFormat: "H:|[scrollView]|", metrics: nil, views: views)
        let greenViewHorizontalConstraint = NSLayoutConstraint.constraints(withVisualFormat: "H:|[scrollView][greenView][scrollView]|", metrics: nil, views: views)
        let greenViewVerticalConstraint = NSLayoutConstraint.constraints(withVisualFormat: "V:|[scrollView][greenView]|", metrics: nil, views: views)

        self.addConstraints(scrollViewHeight)
        self.addConstraints(scrollViewWidth)
        self.addConstraints(greenViewHorizontalConstraint)
        self.addConstraints(greenViewVerticalConstraint)

    }

}

1 个答案:

答案 0 :(得分:0)

我在你的代码中看不到你激活约束的地方。

请注意我们如何激活约束。

//create
let childredView = UIView()
childredView.translatesAutoresizingMaskIntoConstraints = false
childredView.backgroundColor = UIColor.purple

//add it
self.view.addSubview(childredView)
self.view.bringSubview(toFront: childredView)

//set constraints
let views = ["childrenView": childredView]
let vertical = NSLayoutConstraint.constraints(withVisualFormat: "V:|[childrenView]|", options: [], metrics: nil, views: views)
let horizontal = NSLayoutConstraint.constraints(withVisualFormat: "H:|[childrenView]|", options: [], metrics: nil, views: views)
let all = vertical + horizontal

//activate them
NSLayoutConstraint.activate(all)

如果您使用布局锚点,请确保添加isActive = true在您创建的每个YAnchor的末尾。例如:

item.centerYAnchor.constraint(equalTo:self.view.centerYAnchor).isActive = true