{"id":18839753,"url":"https://github.com/maximbilan/uicollectionviewhorizontalpaging","last_synced_at":"2025-10-15T20:43:09.468Z","repository":{"id":76126348,"uuid":"51135057","full_name":"maximbilan/UICollectionViewHorizontalPaging","owner":"maximbilan","description":"iOS UICollectionView Page Scrolling","archived":false,"fork":false,"pushed_at":"2018-09-22T09:37:12.000Z","size":1660,"stargazers_count":73,"open_issues_count":1,"forks_count":12,"subscribers_count":4,"default_branch":"master","last_synced_at":"2025-03-27T20:44:22.356Z","etag":null,"topics":["horizontal","ios","ios-app","paging","swift","uicollectionview"],"latest_commit_sha":null,"homepage":"","language":"Swift","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/maximbilan.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2016-02-05T08:30:06.000Z","updated_at":"2024-05-14T01:43:39.000Z","dependencies_parsed_at":"2023-02-25T05:30:40.220Z","dependency_job_id":null,"html_url":"https://github.com/maximbilan/UICollectionViewHorizontalPaging","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/maximbilan%2FUICollectionViewHorizontalPaging","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/maximbilan%2FUICollectionViewHorizontalPaging/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/maximbilan%2FUICollectionViewHorizontalPaging/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/maximbilan%2FUICollectionViewHorizontalPaging/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/maximbilan","download_url":"https://codeload.github.com/maximbilan/UICollectionViewHorizontalPaging/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248837285,"owners_count":21169374,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["horizontal","ios","ios-app","paging","swift","uicollectionview"],"created_at":"2024-11-08T02:43:58.193Z","updated_at":"2025-10-15T20:43:04.426Z","avatar_url":"https://github.com/maximbilan.png","language":"Swift","readme":"# iOS UICollectionView Page Scrolling\n\nI would like to provide an example how to do a horizontal page scrolling using \u003ci\u003eUICollectionView\u003c/i\u003e.\n\n![alt tag](https://raw.github.com/maximbilan/UICollectionViewHorizontalPaging/master/img/1.gif)\n\nPlease set up \u003ci\u003eisPaggingEnabled\u003c/i\u003e property to \u003cb\u003eYES\u003c/b\u003e for the collection view object. Or via \u003ci\u003eInterface Builder\u003c/i\u003e:\n\n![alt tag](https://raw.github.com/maximbilan/UICollectionViewHorizontalPaging/master/img/2.png)\n\nAnd let’s create a custom class for the flow layout:\n\n![alt tag](https://raw.github.com/maximbilan/UICollectionViewHorizontalPaging/master/img/3.png)\n\nPlease use the following code:\n\n\u003cpre\u003e\nimport UIKit\n\nclass CenterViewFlowLayout: UICollectionViewFlowLayout {\n\t\n\toverride func collectionViewContentSize() -\u003e CGSize {\n\t\t// Only support single section for now.\n\t\t// Only support Horizontal scroll\n\t\tlet count = self.collectionView?.dataSource?.collectionView(self.collectionView!, numberOfItemsInSection: 0)\n\t\tlet canvasSize = self.collectionView!.frame.size\n\t\tvar contentSize = canvasSize\n\t\tif self.scrollDirection == UICollectionViewScrollDirection.Horizontal {\n\t\t\tlet rowCount = Int((canvasSize.height - self.itemSize.height) / (self.itemSize.height + self.minimumInteritemSpacing) + 1)\n\t\t\tlet columnCount = Int((canvasSize.width - self.itemSize.width) / (self.itemSize.width + self.minimumLineSpacing) + 1)\n\t\t\tlet page = ceilf(Float(count!) / Float(rowCount * columnCount))\n\t\t\tcontentSize.width = CGFloat(page) * canvasSize.width\n\t\t}\n\t\treturn contentSize\n\t}\n\t\n\tfunc frameForItemAtIndexPath(indexPath: NSIndexPath) -\u003e CGRect {\n\t\tlet canvasSize = self.collectionView!.frame.size\n\t\tlet rowCount = Int((canvasSize.height - self.itemSize.height) / (self.itemSize.height + self.minimumInteritemSpacing) + 1)\n\t\tlet columnCount = Int((canvasSize.width - self.itemSize.width) / (self.itemSize.width + self.minimumLineSpacing) + 1)\n\t\t\n\t\tlet pageMarginX = (canvasSize.width - CGFloat(columnCount) * self.itemSize.width - (columnCount \u003e 1 ? CGFloat(columnCount - 1) * self.minimumLineSpacing : 0)) / 2.0\n\t\tlet pageMarginY = (canvasSize.height - CGFloat(rowCount) * self.itemSize.height - (rowCount \u003e 1 ? CGFloat(rowCount - 1) * self.minimumInteritemSpacing : 0)) / 2.0\n\t\t\n\t\tlet page = Int(CGFloat(indexPath.row) / CGFloat(rowCount * columnCount))\n\t\tlet remainder = Int(CGFloat(indexPath.row) - CGFloat(page) * CGFloat(rowCount * columnCount))\n\t\tlet row = Int(CGFloat(remainder) / CGFloat(columnCount))\n\t\tlet column = Int(CGFloat(remainder) - CGFloat(row) * CGFloat(columnCount))\n\t\t\n\t\tvar cellFrame = CGRectZero\n\t\tcellFrame.origin.x = pageMarginX + CGFloat(column) * (self.itemSize.width + self.minimumLineSpacing)\n\t\tcellFrame.origin.y = pageMarginY + CGFloat(row) * (self.itemSize.height + self.minimumInteritemSpacing)\n\t\tcellFrame.size.width = self.itemSize.width\n\t\tcellFrame.size.height = self.itemSize.height\n\t\t\n\t\tif self.scrollDirection == UICollectionViewScrollDirection.Horizontal {\n\t\t\tcellFrame.origin.x += CGFloat(page) * canvasSize.width\n\t\t}\n\t\t\n\t\treturn cellFrame\n\t}\n\t\n\toverride func layoutAttributesForItemAtIndexPath(indexPath: NSIndexPath) -\u003e UICollectionViewLayoutAttributes? {\n\t\tlet attr = super.layoutAttributesForItemAtIndexPath(indexPath)?.copy() as! UICollectionViewLayoutAttributes?\n\t\tattr!.frame = self.frameForItemAtIndexPath(indexPath)\n\t\treturn attr\n\t}\n\t\n\toverride func layoutAttributesForElementsInRect(rect: CGRect) -\u003e [UICollectionViewLayoutAttributes]? {\n\t\tlet originAttrs = super.layoutAttributesForElementsInRect(rect)\n\t\tvar attrs: [UICollectionViewLayoutAttributes]? = Array\u003cUICollectionViewLayoutAttributes\u003e()\n\t\t\n\t\tfor attr in originAttrs! {\n\t\t\tlet idxPath = attr.indexPath\n\t\t\tlet itemFrame = self.frameForItemAtIndexPath(idxPath)\n\t\t\tif CGRectIntersectsRect(itemFrame, rect) {\n\t\t\t\tlet nAttr = self.layoutAttributesForItemAtIndexPath(idxPath)\n\t\t\t\tattrs?.append(nAttr!)\n\t\t\t}\n\t\t}\n\t\t\n\t\treturn attrs\n\t}\n\t\n}\n\u003c/pre\u003e\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmaximbilan%2Fuicollectionviewhorizontalpaging","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fmaximbilan%2Fuicollectionviewhorizontalpaging","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmaximbilan%2Fuicollectionviewhorizontalpaging/lists"}