ios - 如何将 UICollectionViews 放入 stackView?
问题描述
我想像这张图片一样将 3 个 collectionViews 放在 stackViews 中。但是,当我模拟它时,我看不到我设计的 collectionViews。
这是我的设计。StackView 中的 3 个 CollectionView
过去,我曾在此站点上尝试过此解决方案,CollectionView Disappears within StackView (Swift)。但是,它不起作用。
class SecondViewController: UIViewController, UICollectionViewDelegate, UICollectionViewDataSource {
let tcells:Int = 3, pcells:Int = 4
var p1name:String = " ", p2name:String = " "
var binScore: Array<Int> = [0,0,0,0]
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
if collectionView == timerReset {
return tcells
}
return pcells
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
if collectionView == p1board {
if let p1nc: p1Name = collectionView.dequeueReusableCell(withReuseIdentifier: "p1Name", for: indexPath) as? p1Name{
p1nc.p1n.text! = p1name
return p1nc
}
if let p1jc: p1Judge = collectionView.dequeueReusableCell(withReuseIdentifier: "p1Judge", for: indexPath) as? p1Judge {
return p1jc
}
if let p1sc: p1Score = collectionView.dequeueReusableCell(withReuseIdentifier: "p1Score", for: indexPath) as? p1Score {
return p1sc
}
}
else if collectionView == p2board {
if let p2nc: p2Name = collectionView.dequeueReusableCell(withReuseIdentifier: "p2Name", for: indexPath) as? p2Name{
p2nc.p2n.text! = p2name
return p2nc
}
if let p2jc: p2Judge = collectionView.dequeueReusableCell(withReuseIdentifier: "p2Judge", for: indexPath) as? p2Judge {
return p2jc
}
if let p2sc: p2Score = collectionView.dequeueReusableCell(withReuseIdentifier: "p2Score", for: indexPath) as? p2Score {
return p2sc
}
}
else {
if let mtm: matchTimer = collectionView.dequeueReusableCell(withReuseIdentifier: "matchTimer", for: indexPath) as? matchTimer{
return mtm
}
if let gtm: groundTimer = collectionView.dequeueReusableCell(withReuseIdentifier: "groundTimer", for: indexPath) as? groundTimer {
return gtm
}
if let rbtn: resetBtn = collectionView.dequeueReusableCell(withReuseIdentifier: "resetBtn", for: indexPath) as? resetBtn {
return rbtn
}
}
return UICollectionViewCell()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
@IBOutlet weak var timerView: UIStackView!
@IBOutlet weak var p1board: UICollectionView! // left side
@IBOutlet weak var timerReset: UICollectionView! // mid
@IBOutlet weak var p2board: UICollectionView! // right side
override func viewDidLoad() {
super.viewDidLoad()
timerView.addArrangedSubview(p1board)
timerView.addArrangedSubview(timerReset)
timerView.addArrangedSubview(p2board)
NSLayoutConstraint.activate([
timerView.leadingAnchor.constraint(equalTo: view.leadingAnchor),
timerView.trailingAnchor.constraint(equalTo: view.trailingAnchor),
timerView.topAnchor.constraint(equalTo: view.topAnchor),
timerView.bottomAnchor.constraint(equalTo: view.bottomAnchor),
p1board.widthAnchor.constraint(equalTo: view.widthAnchor, multiplier: 0.3)
])
}
override var preferredStatusBarStyle: UIStatusBarStyle {
return .lightContent
}
}
解决方案
如果您想以编程方式将约束添加到视图(在本例中为 timerView),您必须这样做:
timerView.translatesAutoresizingMaskIntoConstraints = false
集合视图还需要知道它们的宽度和高度。所以你有两个选择:
1) 为每个集合视图添加高度和宽度约束。
2) 如果您希望集合视图具有相同的宽度,请将堆栈的分布设置为平均填充。
timerView.distribution = .fillEqually
PD:看着你的设计,我认为你应该使用三个 TableViews 而不是 CollectionViews。
推荐阅读
- apache-kafka - Kafka Streams聚合加法器运算符问题
- java - JVM 堆内存分配
- java - 如何使用带有二进制对象的 ignite 数据流传输器,数据未加载到集群
- elasticsearch - 不可用部署副本的值为,0.015,一个浮点数
- javascript - 如何将 D3 时间尺度从 V3 转换为 V4?
- python - 如何检查字符串中是否有特定字符(在 Python 中)
- python - 如何在 PyQt5 中的现有主窗口位置(不是屏幕中心)放置新窗口?
- python - 如何在烧瓶中获取 HttpException 的 URL
- c# - RabbitMQ_Broker 状态
- mysql - ORDER BY datediff 使用多个表