From 7f79db066e53a12346367bdc11c6ce5f2292a902 Mon Sep 17 00:00:00 2001 From: Ian Hoar Date: Mon, 16 May 2016 16:16:50 -0700 Subject: [PATCH] Delay asset controller layout until frame is correct and layout is complete When presenting as a .FormSheet on iPad, the frame is not correct until just after `viewWillAppear:`. Moving the `scrollToItem:` code to viewDidAppear is too late, and viewWillAppear is too early. Dispatching to the main thread waits one run loop until the frame is update and the layout is complete without being visible to the user. --- QBImagePicker/QBAssetsViewController.m | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/QBImagePicker/QBAssetsViewController.m b/QBImagePicker/QBAssetsViewController.m index 37381715..98177684 100644 --- a/QBImagePicker/QBAssetsViewController.m +++ b/QBImagePicker/QBAssetsViewController.m @@ -105,8 +105,12 @@ - (void)viewWillAppear:(BOOL)animated // Scroll to bottom if (self.fetchResult.count > 0 && self.isMovingToParentViewController && !self.disableScrollToBottom) { - NSIndexPath *indexPath = [NSIndexPath indexPathForItem:(self.fetchResult.count - 1) inSection:0]; - [self.collectionView scrollToItemAtIndexPath:indexPath atScrollPosition:UICollectionViewScrollPositionTop animated:NO]; + // when presenting as a .FormSheet on iPad, the frame is not correct until just after viewWillAppear: + // dispatching to the main thread waits one run loop until the frame is update and the layout is complete + dispatch_async(dispatch_get_main_queue(), ^{ + NSIndexPath *indexPath = [NSIndexPath indexPathForItem:(self.fetchResult.count - 1) inSection:0]; + [self.collectionView scrollToItemAtIndexPath:indexPath atScrollPosition:UICollectionViewScrollPositionTop animated:NO]; + }); } }