ios - UIScrollView 没有显示在视图中
问题描述
我正在实施UIScrollView
a CollectionViewCell
。我有一个滚动视图应该显示的自定义视图,因此我在CollectionViewCell
. 我以编程方式创建了所有内容,下面是我的代码:
struct ShotsCollections {
let title: String?
}
class ShotsMainView: UICollectionViewCell {
override init(frame: CGRect) {
super.init(frame: frame)
setupViews()
containerScrollView.contentSize.width = frame.width * CGFloat(shotsData.count)
shotsData = [ShotsCollections.init(title: "squad"), ShotsCollections.init(title: "genral")]
var i = 0
for data in shotsData {
let customview = ShotsMediaView(frame: CGRect(x: containerScrollView.frame.width * CGFloat(i), y: 0, width: containerScrollView.frame.width, height: containerScrollView.frame.height))
containerScrollView.addSubview(customview)
i += 1
}
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
var shotsData = [ShotsCollections]()
var containerScrollView: UIScrollView = {
let instance = UIScrollView()
instance.isScrollEnabled = true
instance.bounces = true
instance.backgroundColor = blueColor
return instance
}()
private func setupViews() { //These are constraints by using TinyConstraints
addSubview(containerScrollView)
containerScrollView.topToSuperview()
containerScrollView.bottomToSuperview()
containerScrollView.rightToSuperview()
containerScrollView.leftToSuperview()
}
}
现在的问题是,虽然显示了滚动视图,但其中的内容却没有。我在打印滚动视图的contentSize
和frame
时,它显示为 0。但是如果我检查调试视图层次结构,滚动视图包含 2 个具有特定框架的视图。
我不确定出了什么问题。任何帮助表示赞赏。
解决方案
当您添加customView
时containerScrollView
,您没有设置 和 之间的customView
约束containerScrollView
。
添加这些约束,您将能够看到您customView
的 s ,因为您customView
有一些高度。此外,当您添加更多视图时,您需要删除最后添加的视图的底部约束,containerScrollView
并为最新添加的视图创建底部约束。
推荐阅读
- c - 尝试使用左移将位值更新为“0”时出错
- php - 来自其他文件的 php 错误
- java - 我需要编写一个 If 语句,如果输入的任何字符包含在字符串中,它将打印一条消息
- python - 如果我已经安装了 python 扩展,如何将 VS Code 中的路径更改为扩展以及 autopep8 的正确路径是什么?
- java - Java 8 Lambdas flatmapping、groupingBy 和 mapping 以获取 T 和 List 的 Map
- javascript - Discord.JS EmbedMessage 与收集器
- elasticsearch - 使用 Node client.update() 处理外部版本控制的正确方法
- python - 如何将 2D numpy 数组转换为 Cython 中的指针数组?
- .net - 从 .Net Framework 4.6 升级到 .Net Framework 4.8 后无法访问 Web 应用程序
- python - 有没有办法解决由 Windows 文件名中的无效字符产生的这些转义序列?