Ios 滚动视图中的内容不是’;当作为子视图添加到集合视图单元格时,不可见
我有一个集合视图,其中包含一个滚动视图作为子视图,我在滚动视图中添加了一个文本视图,但我看不到文本视图。我为集合视图单元格使用了自定义类。代码如下:Ios 滚动视图中的内容不是’;当作为子视图添加到集合视图单元格时,不可见,ios,swift,uicollectionview,uiscrollview,uicollectionviewcell,Ios,Swift,Uicollectionview,Uiscrollview,Uicollectionviewcell,我有一个集合视图,其中包含一个滚动视图作为子视图,我在滚动视图中添加了一个文本视图,但我看不到文本视图。我为集合视图单元格使用了自定义类。代码如下: class CustomReaderPageClass: UICollectionViewCell { fileprivate let scrollView: UIScrollView = { let sv = UIScrollView() sv.backgroundColor = .green
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的初学者。添加这两个约束条件
scrollView.widthAnchor.constraint(equalTo: contentView.widthAnchor, constant: 0).isActive = true
chapterBody.bottomAnchor.constraint(equalTo:scrollView.bottomAnchor, constant: 0).isActive = true
您可以添加UI的图像吗?就像你想要的一样。嗯,你指的是当前视图的图片?
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