如何以编程方式在Swift中将UIView添加到UIScrollView?

时间:2019-03-14 23:15:52

标签: ios swift uiview uiscrollview uikit

我试图仅使用代码向UIScrollView添加视图,但是该视图未出现在UIScrollView中,我不确定为什么。当我添加按钮或标签时,它们就会显示出来。

import UIKit

class profileViewController: UIViewController, UIScrollViewDelegate {

    var label : UILabel = {
        let label = UILabel()
        label.text = "Profile"
        label.textColor = UIColor.init(white: 0.80, alpha: 1)
        label.font = UIFont.systemFont(ofSize: 40)
        label.translatesAutoresizingMaskIntoConstraints = false

        return label
    }()
    var scrollview : UIScrollView = {
        let scrollview = UIScrollView()
        scrollview.translatesAutoresizingMaskIntoConstraints = false
        scrollview.backgroundColor = .clear

        return scrollview
    }()
    var greyview : UIView = {
        let view = UIView()
        view.translatesAutoresizingMaskIntoConstraints =  false
        view.backgroundColor = UIColor.init(white: 0.70, alpha: 1)
        view.backgroundColor = UIColor.gray

        return view
    }()

    override func viewDidLoad() {
        super.viewDidLoad()

        view.backgroundColor = .white
        view.addSubview(label)

        label.centerYAnchor.constraint(equalTo: view.safeAreaLayoutGuide.centerYAnchor).isActive = true
        label.centerXAnchor.constraint(equalTo: view.safeAreaLayoutGuide.centerXAnchor).isActive = true

        scrollview.delegate = self

        view.addSubview(scrollview)
        scrollview.topAnchor.constraint(equalTo: view.safeAreaLayoutGuide.topAnchor).isActive = true
        scrollview.bottomAnchor.constraint(equalTo: view.bottomAnchor).isActive = true
        scrollview.trailingAnchor.constraint(equalTo: view.trailingAnchor).isActive = true
        scrollview.leadingAnchor.constraint(equalTo: view.leadingAnchor).isActive = true
        scrollview.contentSize = CGSize.init(width: view.frame.size.width, height: view.frame.size.height + 500)


        scrollview.addSubview(greyview)
        greyview.topAnchor.constraint(equalTo: scrollview.topAnchor).isActive = true
        greyview.trailingAnchor.constraint(equalTo: scrollview.trailingAnchor).isActive = true
        greyview.leadingAnchor.constraint(equalTo: scrollview.leadingAnchor).isActive = true
        greyview.heightAnchor.constraint(equalToConstant: 100).isActive = true
    }

}

2 个答案:

答案 0 :(得分:0)

这可能是因为您的greyview没有bottomAnchor。它需要顶部和底部锚点才能在scrollView中正常工作:

scrollview.addSubview(greyview)
    greyview.topAnchor.constraint(equalTo: scrollview.topAnchor).isActive = true
    greyview.centerXAnchor.constraint(equalTo: scrollview.centerXAnchor).isActive = true
    greyview.heightAnchor.constraint(equalToConstant: 100).isActive = true
    greyview.widthAnchor.constraint(equalToConstant: 100).isActive = true

添加一个widthAnchor,在这里我将其在滚动视图中居中,但是由您自行决定如何放置它。另外,如果您添加更多项目,只需确保最底部的项目在滚动视图bottomAnchor上附加了bottomAnchor,否则它将不会滚动。

更新:

我不知道您希望greyview的外观如何,但是如果您使高度比scrollView的contentSize高,它将滚动,并确保您拥有{{1 }}:

bottomAnchor

这使得 scrollview.contentSize = CGSize.init(width: view.frame.size.width, height: view.frame.size.height) scrollview.addSubview(greyview) greyview.topAnchor.constraint(equalTo: scrollview.topAnchor).isActive = true greyview.bottomAnchor.constraint(equalTo: scrollview.bottomAnchor).isActive = true greyview.heightAnchor.constraint(equalTo: scrollview.heightAnchor, constant: 500).isActive = true greyview.widthAnchor.constraint(equalTo: scrollview.widthAnchor).isActive = true 的宽度等于scrollview的宽度,高度等于scrollview的高度+ 500,以便滚动。

答案 1 :(得分:0)

这是一种方式:

 self.scrollView = UIScrollView.init()

    if let scrollView = self.scrollView {
        scrollView.showsVerticalScrollIndicator = true
        self.view.add(scrollView)

        scrollView.translatesAutoresizingMaskIntoConstraints = false
        self.addConstraint(UtilConstraint.addTopConstraint(from: scrollView, to: self, value: 0))
        self.addConstraint(UtilConstraint.addBottomConstraint(from: scrollView, to: self, value: 0))
        self.addConstraint(UtilConstraint.addLeftLeftConstraint(from: scrollView, to: self, value: 0))
        self.addConstraint(UtilConstraint.addRightRightConstraint(from: scrollView, to: self, value: 0))

        NSLayoutConstraint.activate(self.constraints)

        greyview.translatesAutoresizingMaskIntoConstraints = false
        scrollView.addSubview(greyview)

        greyview.topAnchor.constraint(equalTo: scrollView.topAnchor).isActive = true
        greyview.leftAnchor.constraint(equalTo: scrollView.leftAnchor).isActive = true
        greyview.bottomAnchor.constraint(equalTo: scrollView.bottomAnchor).isActive = true
        greyview.widthAnchor.constraint(equalTo: scrollView.widthAnchor).isActive = true
    }

请记住,您的greyview应该具有静态定义或通过定义的高度。内部组件。

但是,您缺少的是定义宽度。我已经用widthAnchor完成了。 (假设您需要垂直滚动)