Make UICollectionView scroll right to left?

半城伤御伤魂 提交于 2019-12-13 11:51:20

问题


The natural direction for a UICollectionView to scroll when set horizontally is from left to right. Is there any way to reverse this? The simpler the better.


回答1:


I'm not sure exactly what you mean -- if you set the scrolling to horizontal, it scrolls equally well, left and right. If you want it to start it from the right side, you can use this method:

[self.collectionView scrollToItemAtIndexPath:[NSIndexPath indexPathForItem:self.theData.count - 1 inSection:0] atScrollPosition:UICollectionViewScrollPositionRight animated:NO];

This assumes that you have 1 section, and the array populating the collection view is called theData.




回答2:


Same thing for swift:

collectionView?.scrollToItemAtIndexPath(NSIndexPath(forItem: theData.count - 1, inSection: 0), atScrollPosition: .Right, animated: false)



回答3:


Swift4 solution in cellForItemAt collectionView function

  func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
                let cell = categoryBook.dequeueReusableCell(withReuseIdentifier: "HomeCategoryCell", for: indexPath) as! HomeCategoryCell
                collectionView.transform = CGAffineTransform(scaleX:-1,y: 1);


                cell.transform = CGAffineTransform(scaleX:-1,y: 1);
               }

but this solution in some cases didnt work properly if it dosent you can use colletcion view scrollToItem method and you can implement it after you reload the data .

        self.YourCollectionView.reloadData()
        self.YourCollectionView.scrollToItem(at: NSIndexPath(item: self.YourObjectListData.count - 1, section: 0) as IndexPath, at: .right, animated: false)


来源:https://stackoverflow.com/questions/13784250/make-uicollectionview-scroll-right-to-left

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!