首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >如何以编程方式将UIView添加到Swift中的UIScrollView?

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

Stack Overflow用户
提问于 2019-03-15 07:15:52
回答 2查看 258关注 0票数 0

我试图仅使用代码向UIScrollView添加一个视图,但该视图不会出现在UIScrollView中,我不知道为什么。当我添加一个按钮或标签时,它们就会出现。

代码语言:javascript
复制
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
    }

}
EN

回答 2

Stack Overflow用户

发布于 2019-03-15 07:21:46

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

代码语言:javascript
复制
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,在这里我把它放在滚动视图的中间,但是你可以根据自己的喜好来放置它。另外,如果你添加了更多的项目,只需确保最下面的项目有一个附加到scrollView bottomAnchorbottomAnchor,否则它将不会滚动。

更新:

我不知道你想让你的greyview看起来是什么样子,但如果你设置的高度比scrollView的contentSize高,它就会滚动,并确保你有bottomAnchor

代码语言:javascript
复制
 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宽度等于scrollview width,高度等于scrollview height + 500以便滚动。

票数 0
EN

Stack Overflow用户

发布于 2019-03-15 07:31:50

这是一种方法:

代码语言:javascript
复制
 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做到了这一点。(假设您需要垂直滚动)

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/55173401

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档