简体   繁体   English

如何在Swift,XCode中为Collection View设置自定义水平滚动?

[英]How can I set custom horizontal scroll for Collection View in Swift, XCode?

I want to set custom horizontal scroll for my CollectionView, that it will be scrolling by 1 cell (not by the whole width of my screen). 我想为CollectionView设置自定义水平滚动,它将滚动1个单元格(而不是屏幕的整个宽度)。

I could set HORIZONTAL scroll, but not custom. 我可以设置水平滚动,但不能自定义。 (See screens). (请参阅屏幕)。 1 screen: my extension of my collectionView for UIScrollViewDelegate. 1个屏幕:UIScrollViewDelegate的collectionView的扩展。 *I saw, that in console (see too) "x" - my offset = 290 - it's true! *我看到了,在控制台中(也请参见)“ x”-我的偏移量= 290-是的! But on fact it is not 290. Paging was marked in "true" 2 screen: delegate and dataSource. 但实际上不是290。在“ true” 2屏幕中标记了分页:委托和数据源。

在此处输入图片说明

Help, please! 请帮助!

First you need to set your collectionView scroll direction horizontal ( UICollectionViewScrollDirectionHorizontal ) and set pagingEnabled to YES 首先,你需要设置你的CollectionView滚动方向水平( UICollectionViewScrollDirectionHorizontal ),并设置pagingEnabledYES

It means your collection view will be scroll horizontally with one by one cell. 这意味着您的收藏夹视图将与一个单元格一起水平滚动。

Set horizontal direction (Select collection view from XIB or Storyboard) 设置水平方向(从XIB或情节提要中选择集合视图)

在此处输入图片说明

And for set pagination enable 并启用分页功能

myCollectionView.pagingEnabled = true

Updated : 更新 :

You should use of UICollectionViewDelegateFlowLayout delegate 您应该使用UICollectionViewDelegateFlowLayout委托

func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize
    {
        let width = UIScreen.main.bounds.size.width
        var height = 101.7 // set height as you want
        return CGSize(width: width, height: height)
    }

If using storyBoard the go to collection view and select the right handed tab "show the size inspector" and change 如果使用StoryBoard,请转到“收藏”视图,然后选择右手选项卡“显示尺寸检查器”并进行更改

minimum spacing for cells = 0
minimum spacing for lines = 0

if you want to change directly from code then implement these UICollectionViewDelegateFlowLayout methods 如果您想直接从代码中更改,则实现这些UICollectionViewDelegateFlowLayout方法

 func collectionView(_ collectionView: UICollectionView, layout 
       collectionViewLayout: UICollectionViewLayout, 
   minimumLineSpacingForSectionAt section: Int) -> CGFloat {
    return 0
}

func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumInteritemSpacingForSectionAt section: Int) -> CGFloat {
    return 0
}

must be remember scrolling direction set horizontal and pagination enabled 必须记住滚动方向设置为水平并启用分页

and implement this delegate methods into code 并将此委托方法实现为代码

  extension ViewController: UICollectionViewDelegateFlowLayout {
 func collectionView(_ collectionView: UICollectionView, layout 
 collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: 
 IndexPath) -> CGSize {
      return CGSize(width: collectionView.frame.size.width, height: 
collectionView.frame.size.height)
     }
 }

You could create a custom UICollectionViewFlowLayout that will center the cells in the screen and then page one at a time. 您可以创建一个自定义UICollectionViewFlowLayout,它将在屏幕中居中放置单元格,然后一次翻页。 Karmadust have written a good blog post that I have successfully used in the past to do the same thing Karmadust撰写了一篇不错的博客文章,我过去已经成功地使用它来做同样的事情

http://blog.karmadust.com/centered-paging-with-preview-cells-on-uicollectionview/ http://blog.karmadust.com/centered-paging-with-preview-cells-on-uicollectionview/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 集合视图在设置行数后水平滚动? iOS Swift - collection view horizontal scroll after set number of rows ? ios Swift Swift Collection 视图水平和垂直滚动 - Swift Collection view Horizontal and vertical scroll 我如何快速在集合视图中加载多个自定义视图 - How can i load multiple custom views in collection view swift 如何在 Swift 中的集合视图上方添加水平按钮堆栈? - How can I add a horizontal stack of buttons above a collection view in Swift? 集合视图中的水平滚动 - Horizontal scroll in collection view 如何使用标题水平滚动集合视图 - How to scroll the collection view horizontal with header Swift-水平滚动收集视图单元(自动和手动) - Swift - horizontal scroll collection view cells (automatically and manually) 如何在 Xcode 中使用 Swift 为每个循环实现滚动视图? - How can I implement a scroll view while a for each loop with Swift in Xcode? 如何更改水平滚动集合视图中单元格之间的水平间距 - How do I change the horizontal spacing between cells in a horizontal scroll collection view Xcode Swift集合视图:每当点击按钮时,如何在部分中添加项目数? - Xcode Swift Collection View: How can I add the number of items in section whenever the button is tapped?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM