带有AutoLayout的ScrollView不起作用

时间:2018-07-19 20:12:38

标签: ios swift uicollectionview uikit scrollview

我在UIScrollView上很难以编程方式添加子视图,我看过很多教程,但它们都集中在情节提要上,并没有真正解决我的问题,我只是想在一个视图中添加一些子视图。 scrollView但不会在设备上显示任何内容,它只显示scrollView而没有我的子视图,我试图做这样的事情:

view.addSubview(mainScrollView)

mainScrollView.anchor(top: view.safeAreaLayoutGuide.topAnchor, right: view.rightAnchor, bottom: view.bottomAnchor, left: view.leftAnchor, topPadding: 10, rightPadding: 0, bottomPadding: 0, leftPadding: 0, width: 0, height: 0)

let v1 = UIView()
v1.backgroundColor = .blue

let v2 = UIView()
v2.backgroundColor = .black

let v3 = UIView()
v3.backgroundColor = .yellow

let v4 = UIView()
v4.backgroundColor = .green


mainScrollView.addSubview(v1)
mainScrollView.addSubview(v2)
mainScrollView.addSubview(v3)
mainScrollView.addSubview(v4)


v1.anchor(top: mainScrollView.topAnchor, right: nil, bottom: nil, left: nil, topPadding: 0, rightPadding: 0, bottomPadding: 0, leftPadding: 0, width: mainScrollView.contentSize.width, height: 0)

v2.anchor(top: v1.bottomAnchor, right: nil, bottom: nil, left: nil, topPadding: 0, rightPadding: 0, bottomPadding: 0, leftPadding: 0, width: mainScrollView.contentSize.width, height: 0)

v3.anchor(top: v2.bottomAnchor, right: nil, bottom: nil, left: nil, topPadding: 0, rightPadding: 0, bottomPadding: 0, leftPadding: 0, width: mainScrollView.contentSize.width, height: 0)

v4.anchor(top: v3.bottomAnchor, right: nil, bottom: mainScrollView.bottomAnchor, left: nil, topPadding: 0, rightPadding: 0, bottomPadding: 0, leftPadding: 0, width: mainScrollView.contentSize.width, height: 0)

anchor只是一个辅助扩展,可以更轻松地约束和激活约束

3 个答案:

答案 0 :(得分:2)

1-任何有约束的程序化视图都应

v.translateAutoresizingMaskIntoconstarints = false

2-您没有给v1 ... 4设置高度限制,请检查

import UIKit

class ViewController: UIViewController {

    let scrollView = UIScrollView()

    override func viewDidLoad() {

        super.viewDidLoad()

        let viewsCount = 7

        var prevView = self.view!

        scrollView.translatesAutoresizingMaskIntoConstraints = false

        view.addSubview(scrollView)

          NSLayoutConstraint.activate([

            scrollView.leadingAnchor.constraint(equalTo: view.leadingAnchor),
            scrollView.trailingAnchor.constraint(equalTo: view.trailingAnchor),
            scrollView.topAnchor.constraint(equalTo: view.topAnchor,constant:20),
            scrollView.bottomAnchor.constraint(equalTo: view.bottomAnchor),

         ])

        for i in 0..<viewsCount {

            let myView = UIView()

            myView.backgroundColor =  (i % 2 == 0 ) ? .red : .green

            myView.translatesAutoresizingMaskIntoConstraints = false

            scrollView.addSubview(myView)

            if prevView == self.view {

                myView.topAnchor.constraint(equalTo: scrollView.topAnchor).isActive = true
            }
            else {

                myView.topAnchor.constraint(equalTo: prevView.bottomAnchor).isActive = true
            }

            NSLayoutConstraint.activate([

                myView.widthAnchor.constraint(equalToConstant: self.view.frame.width),
                myView.heightAnchor.constraint(equalToConstant: 400)

            ])


            if i == viewsCount - 1  {

                myView.bottomAnchor.constraint(equalTo: scrollView.bottomAnchor).isActive = true
            }

            prevView = myView


        } 
    }
}

答案 1 :(得分:0)

因为您正在初始化没有框架的子视图,所以您的视图似乎没有高度,除非锚定功能执行某些操作以赋予其高度。您可以在扩展名中共享代码吗?

答案 2 :(得分:0)

这是扩展名

extension UIView {
func anchor(top: NSLayoutYAxisAnchor?, right: NSLayoutXAxisAnchor?, bottom: NSLayoutYAxisAnchor?, left: NSLayoutXAxisAnchor?, topPadding: CGFloat, rightPadding: CGFloat, bottomPadding: CGFloat, leftPadding: CGFloat, width: CGFloat, height: CGFloat) {

    translatesAutoresizingMaskIntoConstraints = false

    if let top = top {
        self.topAnchor.constraint(equalTo: top, constant: topPadding).isActive = true
    }
    if let right = right {
        self.rightAnchor.constraint(equalTo: right, constant: -rightPadding).isActive = true
    }
    if let bottom = bottom {
        self.bottomAnchor.constraint(equalTo: bottom, constant: -bottomPadding).isActive = true
    }
    if let left = left {
        self.leftAnchor.constraint(equalTo: left, constant: leftPadding).isActive = true
    }

    if width != 0 {
        self.widthAnchor.constraint(equalToConstant: width).isActive = true
    }
    if height != 0 {
        self.heightAnchor.constraint(equalToConstant: height).isActive = true
    }

}

}