簡體   English   中英

如何以編程方式在Swift中將UIView添加到UIScrollView?

[英]How do I add a UIView to a UIScrollView In Swift programmatically?

我試圖僅使用代碼向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
    }

}

這可能是因為你的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附加到scrollView bottomAnchor或它不會滾動。

更新:

我不知道你希望你的greyview看起來如何,但如果你的高度比scrollView的contentSize高,它會滾動,並確保你有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

這使得greyview寬度等於滾動視圖寬度,高度等於滾動視圖高度+ 500,以便滾動。

這是一種方法:

 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完成了它。 (假設您需要垂直滾動)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM