让我分享截图.
第一张图片描述首次开放.第二个图像描述了滚动问题.
第三张图片描述了我的故事板.
谢谢你的帮助.
这是我的故事板
我的代码
//// VIEwController.swift// ScrollCollectionVIEw//// Created by Cbs on 31.05.2017.// copyright © 2017 Cbs. All rights reserved.//import UIKitclass 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 App的行为.我想让我的应用程序像这个视频(https://youtu.be/Nm-sjXVEL5s)
解决方法 根据您的要求,无需使用UIScrollVIEw.您可以使用UICollectionVIEw创建屏幕.要显示可滚动标题,可以使用UICollectionReusableVIEw作为集合视图的节标题.
1.UICollection视图代码:
import UIKitclass PinterestVIEwController: UIVIEwController{ overrIDe func vIEwDIDLoad() { super.vIEwDIDLoad() }}extension PinterestVIEwController : UICollectionVIEwDataSource,UICollectionVIEwDelegate{ 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) return cell } func collectionVIEw(_ collectionVIEw: UICollectionVIEw,vIEwForSupplementaryElementOfKind kind: String,at indexPath: IndexPath) -> UICollectionReusableVIEw { if kind == UICollectionElementKindSectionheader { let headerVIEw = collectionVIEw.dequeueReusableSupplementaryVIEw(ofKind: UICollectionElementKindSectionheader,withReuseIDentifIEr: "headerVIEw",for: indexPath) return headerVIEw } return UICollectionReusableVIEw() }}extension PinterestVIEwController : UICollectionVIEwDelegateFlowLayout{ func collectionVIEw(_ collectionVIEw: UICollectionVIEw,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.界面:
3.输出屏幕:
编辑:
在提供的代码中:https://www.raywenderlich.com/107439/uicollectionview-custom-layout-tutorial-pinterest
做一些改变:
class PinterestLayout: UICollectionVIEwFlowLayout{ //... overrIDe func prepare() { // 1 if cache.isEmpty { //... var yOffset = [CGfloat](repeating: 150,count: numberOfColumns) //Height of your header vIEw //... } } overrIDe var collectionVIEwContentSize: CGSize { return CGSize(wIDth: contentWIDth,height: contentHeight) } overrIDe func layoutAttributesForElements(in rect: CGRect) -> [UICollectionVIEwLayoutAttributes]? { guard let attributes = super.layoutAttributesForElements(in: rect) else { return nil } var layoutAttributes = [UICollectionVIEwLayoutAttributes]() for attr in attributes where attr.representedElementKind == UICollectionElementKindSectionheader { if let supplementaryAttributes = layoutAttributesForSupplementaryVIEw(ofKind: UICollectionElementKindSectionheader,at: attr.indexPath) { layoutAttributes.append(supplementaryAttributes) } } for attributes in cache { if attributes.frame.intersects(rect) { layoutAttributes.append(attributes) } } return layoutAttributes } overrIDe func layoutAttributesForSupplementaryVIEw(ofKind elementKind: String,at indexPath: IndexPath) -> UICollectionVIEwLayoutAttributes? { if elementKind == UICollectionElementKindSectionheader { let attributes = UICollectionVIEwLayoutAttributes(forSupplementaryVIEwOfKind: UICollectionElementKindSectionheader,with: indexPath) attributes.frame = CGRect(x: 0,y: 0,wIDth: self.contentWIDth,height: 150) //Height of your header vIEw return attributes } return nil }}
另外在Collection VIEw Interface中:
总结以上是内存溢出为你收集整理的ios – UICollectionView高度不能使用UIScrollView内部动态递增全部内容,希望文章能够帮你解决ios – UICollectionView高度不能使用UIScrollView内部动态递增所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)