当作为子视图添加到集合视图单元格时,滚动视图中的内容不可见

Posted

技术标签:

【中文标题】当作为子视图添加到集合视图单元格时,滚动视图中的内容不可见【英文标题】:Contents in the scroll view aren’t visible when added as a sub view to the collection view cell 【发布时间】:2019-09-12 10:27:59 【问题描述】:

我有一个集合视图,其中包含一个作为子视图的滚动视图,我已经向滚动视图添加了一个文本视图,但我看不到文本视图。我为集合视图单元格使用了一个自定义类。这是代码:

class CustomReaderPageClass: UICollectionViewCell 

    fileprivate let scrollView: UIScrollView = 

        let sv = UIScrollView()
        sv.backgroundColor = .green
        sv.contentSize.height = 1200
        return sv

    ()

    fileprivate let chapterBody: UITextView = 

        let textView = UITextView()
        textView.text = "2019"
        textView.font = UIFont(name: "Avenir", size: 18)
        textView.isEditable = false
        textView.backgroundColor = .yellow//UIColor.init(red: 210/255, green: 198/255, blue: 194/255, alpha: 1)
        return textView

    ()

    override init(frame: CGRect) 
        super.init(frame: frame)

        contentView.addSubview(scrollView)

        scrollView.translatesAutoresizingMaskIntoConstraints = false
        scrollView.topAnchor.constraint(equalTo: contentView.topAnchor, constant: 0).isActive = true
        scrollView.leadingAnchor.constraint(equalTo: contentView.leadingAnchor, constant: 0).isActive = true
        scrollView.trailingAnchor.constraint(equalTo: contentView.trailingAnchor, constant: 0).isActive = true
        scrollView.bottomAnchor.constraint(equalTo: contentView.bottomAnchor, constant: 0).isActive = true

        scrollView.addSubview(chapterBody)
        // Add the constraints to the chapter body text view
        chapterBody.translatesAutoresizingMaskIntoConstraints = false
        chapterBody.topAnchor.constraint(equalTo: scrollView.safeAreaLayoutGuide.topAnchor, constant: 10).isActive = true
        chapterBody.leadingAnchor.constraint(equalTo: contentView.leadingAnchor).isActive = true
        chapterBody.trailingAnchor.constraint(equalTo: contentView.trailingAnchor).isActive = true
        chapterBody.heightAnchor.constraint(equalToConstant: 300)

    

我是 Swift 的初学者。

【问题讨论】:

可以添加 UI 的图片吗?就像你想要的那样。 嗯,你是说当前视图的图片? 【参考方案1】:

添加这两个约束

    scrollView.widthAnchor.constraint(equalTo: contentView.widthAnchor, constant: 0).isActive = true
    chapterBody.bottomAnchor.constraint(equalTo:scrollView.bottomAnchor, constant: 0).isActive = true

    scrollView.translatesAutoresizingMaskIntoConstraints = false
    scrollView.topAnchor.constraint(equalTo: contentView.topAnchor, constant: 0).isActive = true
    scrollView.leadingAnchor.constraint(equalTo: contentView.leadingAnchor, constant: 0).isActive = true
    scrollView.trailingAnchor.constraint(equalTo: contentView.trailingAnchor, constant: 0).isActive = true
    scrollView.bottomAnchor.constraint(equalTo: contentView.bottomAnchor, constant: 0).isActive = true
    scrollView.widthAnchor.constraint(equalTo: contentView.widthAnchor, constant: 0).isActive = true

    scrollView.addSubview(chapterBody)
    // Add the constraints to the chapter body text view
    chapterBody.translatesAutoresizingMaskIntoConstraints = false
    chapterBody.topAnchor.constraint(equalTo: scrollView.safeAreaLayoutGuide.topAnchor, constant: 10).isActive = true
    chapterBody.leadingAnchor.constraint(equalTo: scrollView.leadingAnchor).isActive = true
    chapterBody.trailingAnchor.constraint(equalTo: scrollView.trailingAnchor).isActive = true
    chapterBody.heightAnchor.constraint(equalToConstant: 300)
    chapterBody.bottomAnchor.constraint(equalTo:scrollView.bottomAnchor, constant: 0).isActive = true

【讨论】:

以上是关于当作为子视图添加到集合视图单元格时,滚动视图中的内容不可见的主要内容,如果未能解决你的问题,请参考以下文章

生成新单元格时清除集合视图单元格(iOS7 - AFNetworking)

编辑另一个单元格时,集合视图会自动跳转到第一个单元格

表格单元格中的集合视图。这是重用的错误

获取嵌入在滚动视图中的单元格的中心点

滚动到顶部时在表格视图的开头动态添加行

在我的视图集合中,我添加了一个搜索栏,它过滤了单元格,但是当我单击单元格时,它自己的路径没有改变