[英]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
),并设置pagingEnabled
为YES
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.