Swift 在具有集合视图标题和水平流布局的集合视图中嵌入表视图时进行垂直和水平滚动
我正在尝试实现一个应用程序,允许用户在集合视图单元格之间水平滑动,同时也能够垂直滚动查看特定单元格的全部内容 我希望所有集合视图单元格中都嵌入一个表视图 到目前为止,我遇到的问题是,我的水平滚动工作正常,因为我已将集合视图流布局设置为水平。我知道集合视图流布局只能支持一个方向。因此,我尝试实现以下解决方案 现在在我的项目中,我有一个视图控制器,里面有一个滚动视图。嵌入在滚动视图中的是集合视图。此集合视图通过dequeueReusableSupplementaryView实现自己的头 我知道约束经常会成为阻止垂直滚动的问题,因此下面是我的约束图片: 此外,以下是我用于实现此系统的一些代码: 确定滚动视图内容大小Swift 在具有集合视图标题和水平流布局的集合视图中嵌入表视图时进行垂直和水平滚动,swift,uitableview,uicollectionview,uiscrollview,horizontal-scrolling,Swift,Uitableview,Uicollectionview,Uiscrollview,Horizontal Scrolling,我正在尝试实现一个应用程序,允许用户在集合视图单元格之间水平滑动,同时也能够垂直滚动查看特定单元格的全部内容 我希望所有集合视图单元格中都嵌入一个表视图 到目前为止,我遇到的问题是,我的水平滚动工作正常,因为我已将集合视图流布局设置为水平。我知道集合视图流布局只能支持一个方向。因此,我尝试实现以下解决方案 现在在我的项目中,我有一个视图控制器,里面有一个滚动视图。嵌入在滚动视图中的是集合视图。此集合视图通过dequeueReusableSupplementaryView实现自己的头 我知道约束经
override func viewDidLayoutSubviews() {
super.viewWillLayoutSubviews()
scrollView.contentSize.height = collectionView.frame.size.height
scrollView.contentSize.width = self.view.frame.size.width
}
设置集合视图
func setupCollectionView() {
collectionView.dataSource = self
collectionView.delegate = self
collectionView.register(UINib(nibName: "TableViewHolderCollectionViewCell", bundle: nil), forCellWithReuseIdentifier: "tableViewHolderCollectionViewCell")
collectionView.register(UINib(nibName: "CustomCollectionViewHeaderView", bundle: nil), forSupplementaryViewOfKind: UICollectionView.elementKindSectionHeader, withReuseIdentifier: "CustomCollectionViewHeaderView")
collectionView.delaysContentTouches = true
collectionView.contentInsetAdjustmentBehavior = .never
collectionView.bounces = false
collectionView.isPagingEnabled = true
let layout = UICollectionViewFlowLayout()
layout.scrollDirection = .horizontal
layout.minimumLineSpacing = 0
layout.sectionHeadersPinToVisibleBounds = true
layout.sectionInset = UIEdgeInsets(top: 0, left: -self.view.frame.size.width, bottom: 0, right: 0)
collectionView.setCollectionViewLayout(layout, animated: false)
}
设置集合视图数据
extension TasksAndScheduleViewController: UICollectionViewDelegate, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout {
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return 8
}
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
return CGSize(width: self.view.frame.size.width, height: self.view.frame.size.height)
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "tableViewHolderCollectionViewCell", for: indexPath) as! TableViewHolderCollectionViewCell
cell.backgroundColor = colorArray[indexPath.row]
return cell
}
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, referenceSizeForHeaderInSection section: Int) -> CGSize {
return CGSize(width: collectionView.frame.width, height: 343)
}
func collectionView(_ collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, at indexPath: IndexPath) -> UICollectionReusableView {
//setting up header view
return headerView
}
}
在尝试调试之后,我意识到集合视图垂直滚动可能覆盖了我添加到视图控制器中的滚动视图的垂直滚动。为了解决这个问题,我创建了一个自定义类(如下所示),我的集合视图实现了该类。据我所知,这成功地禁用了集合视图的垂直滚动,但未能成功启用其他滚动视图的垂直滚动
class CollectionViewVerticalScroll: UICollectionView {
override func gestureRecognizerShouldBegin(_ gestureRecognizer: UIGestureRecognizer) -> Bool {
let direction = panGestureRecognizer.direction(in: self)
if direction.contains(.Down) || direction.contains(.Up) {
return false
}
return true
}
}
我期望的目标是能够在这些具有表视图的集合视图单元格之间水平滑动,同时能够垂直滚动集合视图单元格及其内部嵌入的表视图内容。理想情况下,垂直滚动应该允许我滚动表格视图单元格,同时也可以向上移动整个视图。我在网上找到的最接近我正在尝试实现的例子是twitter的搜索页面。唯一的区别是我的应用程序有一个集合标题视图,没有导航栏。我在下面附上了一张照片:
我将感谢任何帮助。如果您有任何问题或有什么不明白的地方,请务必告诉我