【发布时间】:2017-11-01 03:50:33
【问题描述】:
我正在开发一个为用户显示照片的应用。我的设计就像 Pinterest 应用程序。我在UIScrollView 中添加了UICollectionView 并禁用了UICollectionView 的滚动。但是当我使用像 UICollectionView 这样的组件时,根据它的内容不能滚动。只有可见的显示,其他的不显示。这个项目的主要痛点是 CollectionViews 的内容是动态的。它可能会随着用户交互而改变。我的问题是如何使用 UIScrollView 完全滚动 UICollectionView 及其动态内容。
让我分享截图。
第一张图片描述了第一次打开。第二张图片描述了滚动问题。 第三张图片描述了我的故事板。
感谢您的帮助。
这是我的故事板
还有我的代码
//
// ViewController.swift
// ScrollCollectionView
//
// Created by Cbs on 31.05.2017.
// Copyright © 2017 Cbs. All rights reserved.
//
import UIKit
class ViewController: UIViewController, UICollectionViewDelegate, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout {
@IBOutlet weak var collectionView: UICollectionView!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return 25
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cell", for: indexPath) as! CollectionViewCell
cell.addShadow(opacitiy: 1, shadowRadius: 3, shadowOffsetWidth: 2, shadowOffsetHeight: 2, shadowColor: UIColor.lightGray, backgroundColor: UIColor.red)
cell.addCornerRadius(cornerRadius: 8)
return cell
}
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
let cellSpacing = CGFloat(2) //Define the space between each cell
let leftRightMargin = CGFloat(40) //If defined in Interface Builder for "Section Insets"
let numColumns = CGFloat(2) //The total number of columns you want
let totalCellSpace = cellSpacing * (numColumns - 1)
let screenWidth = UIScreen.main.bounds.width
let width = (screenWidth - leftRightMargin - totalCellSpace) / numColumns
let height = CGFloat(210) //whatever height you want
return CGSize(width: width, height: height) // width & height are the same to make a square cell
}
}
编辑:
我想我无法清楚地解释我的问题。在这里,我将提供一些额外的视频来清楚地说明这个问题。我上传了2个视频。
在此视频中,我的标题没有与 collectionview 子项一起滚动。所以我想滚动屏幕中的整个元素。 (https://youtu.be/0ArQe6mZytc)
在这个短视频中,您可以看到滚动时 Pinterest 应用程序的行为。我想让我的应用像这个视频一样 (https://youtu.be/Nm-sjXVEL5s)
【问题讨论】:
-
为什么在滚动视图集合视图中添加集合视图是滚动视图本身的子类。
-
我的建议是不要在 UIScrollView 中包含 UICollectionView,因为 UICollectionView 已经是 UIScrollView 的子类。使用 CollectionView 标头在顶部添加您的图像和标签。
-
@Imad 但我必须为我的功能使用自定义标头。使用自定义布局标题部分时消失了:S
-
@salih 集合视图具有补充标题视图功能。
-
@salih 看看这个为你的集合视图实现一个标题:stackoverflow.com/questions/21731318/…
标签: ios swift scroll uiscrollview uicollectionview