UICollectionView从右向左滚动(反向)

Chr*_*sen 5 uiscrollview ios uistoryboard uicollectionview

我希望有一个从右向左滚动的UICollectionView,即当它在加载后出现在屏幕上时,收集视图应该首先显示最右边的单元格/项目,然后在左边添加其余的.我已经尝试了这里提供的解决方法,但是如果我打电话给viewWillAppear:我,我得到:

*** Assertion failure in -[UICollectionViewData layoutAttributesForItemAtIndexPath:], /SourceCache/UIKit/UIKit-2372/UICollectionViewData.m:485
Run Code Online (Sandbox Code Playgroud)

如果我滚动到它中的最后一项viewDidAppear:工作正常,除了现在用户首先看到左项,然后滚动到最后一项.使用也试过contentOffset在UICollectionView财产(因为它的UIScrollView的一个子类),但这个参数也只设置在某个时候之间viewWillAppear:viewDidAppear:

任何替代品?我想我可以尝试子类化UICollectionViewFlowLayout并从右到左布局单元格,但我有点急于进入那个领域.

这就是我做的:

- (void)_scrollToTheRight
{
    NSIndexPath *lastIndexPath = [self.fetchedResultsController indexPathForObject:self.fetchedResultsController.fetchedObjects.lastObject];
    if (lastIndexPath)
    {
        [self.collectionView scrollToItemAtIndexPath:lastIndexPath atScrollPosition:UICollectionViewScrollPositionRight animated:NO];
    }
}
Run Code Online (Sandbox Code Playgroud)

每个值是:

lastIndexPath
(NSIndexPath *) $0 = 0x1f1754a0 <NSIndexPath 0x1f1754a0> 2 indexes [0, 21]

(NSInteger)[self.fetchedResultsController.fetchedObjects count]
(NSInteger) $3 = 22 [no Objective-C description available]

(NSInteger)[self.collectionView numberOfItemsInSection:0]
(NSInteger) $2 = 22 [no Objective-C description available]
Run Code Online (Sandbox Code Playgroud)

最后一点:集合视图控制器是从UIPageViewController放在屏幕上的视图控制器的容器视图(iOS 6故事板中的新内容)加载的.

谢谢.

编辑1: 所以我认为问题是使用我的故事板布局(UICollectionViewController使用新的容器视图嵌入)导致问题. 在此输入图像描述 因为应用程序中的其他地方我也嵌入了视图控制器(正常UITableViewController)viewDidAppear:在视图实际出现之前被调用.我怀疑这个设置没有正确通知每个子视图控制器他们应该加载和布局他们的视图.

bbj*_*jay 3

从 iOS 9 开始就可以使用

collectionView.semanticContentAttribute = .forceRightToLeft
Run Code Online (Sandbox Code Playgroud)

用于从右向左滚动,scrollDirection = .horizontal并且 itemSize 填充整个高度(即只有一个水平行的项目)

  • 旁注:为了使此解决方案正常工作,您需要反转数据源中的排序顺序才能看到预期的结果。 (2认同)