diff --git a/NohanaImagePicker/AlbumListViewController.swift b/NohanaImagePicker/AlbumListViewController.swift index a773c58..538b5b5 100644 --- a/NohanaImagePicker/AlbumListViewController.swift +++ b/NohanaImagePicker/AlbumListViewController.swift @@ -51,6 +51,11 @@ class AlbumListViewController: UITableViewController, EmptyIndicatable, Activity tableView.deselectRowAtIndexPath(indexPathForSelectedRow, animated: true) } } + + override func viewWillTransitionToSize(size: CGSize, withTransitionCoordinator coordinator: UIViewControllerTransitionCoordinator) { + super.viewWillTransitionToSize(size, withTransitionCoordinator: coordinator) + tableView.reloadData() + } // MARK: - UITableViewDelegate @@ -160,9 +165,10 @@ class AlbumListViewController: UITableViewController, EmptyIndicatable, Activity mediaType: nohanaImagePickerController.mediaType, shouldShowEmptyAlbum: nohanaImagePickerController.shouldShowEmptyAlbum, handler: { () -> Void in - dispatch_async(dispatch_get_main_queue(), { () -> Void in - momentViewController.isLoading = false - momentViewController.collectionView?.reloadData() + dispatch_async(dispatch_get_main_queue(), { [weak momentViewController] in + momentViewController?.isLoading = false + momentViewController?.collectionView?.reloadData() + momentViewController?.scrollCollectionViewToInitialPosition() }) }) case .Albums: diff --git a/NohanaImagePicker/AnimatableNavigationController.swift b/NohanaImagePicker/AnimatableNavigationController.swift index a39020c..d75f9a5 100644 --- a/NohanaImagePicker/AnimatableNavigationController.swift +++ b/NohanaImagePicker/AnimatableNavigationController.swift @@ -8,7 +8,7 @@ import UIKit -class AnimatableNavigationController: UINavigationController, UINavigationControllerDelegate, UIViewControllerTransitioningDelegate { +class AnimatableNavigationController: UINavigationController, UINavigationControllerDelegate { let swipeInteractionController = SwipeInteractionController() diff --git a/NohanaImagePicker/AssetCell.swift b/NohanaImagePicker/AssetCell.swift index 957bd1e..0ed54d9 100644 --- a/NohanaImagePicker/AssetCell.swift +++ b/NohanaImagePicker/AssetCell.swift @@ -29,7 +29,7 @@ class AssetCell: UICollectionViewCell { } @IBAction func didPushPickButton(sender: UIButton) { - guard let asset = asset, nohanaImagePickerController = nohanaImagePickerController else { + guard let asset = asset else { return } if pickButton.selected { diff --git a/NohanaImagePicker/AssetDetailListViewController.swift b/NohanaImagePicker/AssetDetailListViewController.swift index d6355d9..e5e92db 100644 --- a/NohanaImagePicker/AssetDetailListViewController.swift +++ b/NohanaImagePicker/AssetDetailListViewController.swift @@ -36,8 +36,18 @@ class AssetDetailListViewController: AssetListViewController { forState: [.Normal, .Selected]) } - override func viewWillAppear(animated: Bool) { - super.viewWillAppear(animated) + override func viewWillTransitionToSize(size: CGSize, withTransitionCoordinator coordinator: UIViewControllerTransitionCoordinator) { + let indexPath = currentIndexPath + view.hidden = true + coordinator.animateAlongsideTransition(nil) { _ in + self.view.invalidateIntrinsicContentSize() + for subView in self.view.subviews { + subView.invalidateIntrinsicContentSize() + } + self.collectionView?.reloadData() + self.scrollCollectionView(to: indexPath) + self.view.hidden = false + } } override func updateTitle() { @@ -51,16 +61,21 @@ class AssetDetailListViewController: AssetListViewController { nohanaImagePickerController.delegate?.nohanaImagePicker?(nohanaImagePickerController, assetDetailListViewController: self, didChangeAssetDetailPage: indexPath, photoKitAsset: asset.originalAsset) } + override func scrollCollectionView(to indexPath: NSIndexPath) { + guard photoKitAssetList.count > 0 else { + return + } + let toIndexPath = NSIndexPath(forItem: indexPath.item, inSection: 0) + collectionView?.scrollToItemAtIndexPath(toIndexPath, atScrollPosition: UICollectionViewScrollPosition.CenteredHorizontally, animated: false) + } + override func scrollCollectionViewToInitialPosition() { guard isFirstAppearance else { return } - isFirstAppearance = false - guard photoKitAssetList.count > 0 else { - return - } let indexPath = NSIndexPath(forRow: currentIndexPath.item, inSection: 0) - collectionView?.scrollToItemAtIndexPath(indexPath, atScrollPosition: UICollectionViewScrollPosition.CenteredHorizontally, animated: false) + scrollCollectionView(to: indexPath) + isFirstAppearance = false } // MARK: - IBAction @@ -81,11 +96,11 @@ class AssetDetailListViewController: AssetListViewController { // MARK: - UICollectionViewDelegate override func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell { - guard let cell = collectionView.dequeueReusableCellWithReuseIdentifier("AssetDetailCell", forIndexPath: indexPath) as? AssetDetailCell, - nohanaImagePickerController = nohanaImagePickerController + guard let cell = collectionView.dequeueReusableCellWithReuseIdentifier("AssetDetailCell", forIndexPath: indexPath) as? AssetDetailCell else { fatalError("failed to dequeueReusableCellWithIdentifier(\"AssetDetailCell\")") } + cell.invalidateIntrinsicContentSize() cell.scrollView.zoomScale = 1 cell.tag = indexPath.item @@ -115,7 +130,13 @@ class AssetDetailListViewController: AssetListViewController { return } let row = Int((collectionView.contentOffset.x + cellSize.width * 0.5) / cellSize.width) - currentIndexPath = NSIndexPath(forRow: row, inSection: currentIndexPath.section) + if row < 0 { + currentIndexPath = NSIndexPath(forRow: 0, inSection: currentIndexPath.section) + } else if row >= collectionView.numberOfItemsInSection(0) { + currentIndexPath = NSIndexPath(forRow: collectionView.numberOfItemsInSection(0) - 1, inSection: currentIndexPath.section) + } else { + currentIndexPath = NSIndexPath(forRow: row, inSection: currentIndexPath.section) + } } // MARK: - UICollectionViewDelegateFlowLayout diff --git a/NohanaImagePicker/AssetListViewController.swift b/NohanaImagePicker/AssetListViewController.swift index 66f5b54..fd50a83 100644 --- a/NohanaImagePicker/AssetListViewController.swift +++ b/NohanaImagePicker/AssetListViewController.swift @@ -41,31 +41,49 @@ class AssetListViewController: UICollectionViewController { override func viewWillAppear(animated: Bool) { super.viewWillAppear(animated) setToolbarTitle(nohanaImagePickerController) + view.invalidateIntrinsicContentSize() + for subView in view.subviews { + subView.invalidateIntrinsicContentSize() + } collectionView?.reloadData() + scrollCollectionViewToInitialPosition() } + override func viewWillTransitionToSize(size: CGSize, withTransitionCoordinator coordinator: UIViewControllerTransitionCoordinator) { + super.viewWillTransitionToSize(size, withTransitionCoordinator: coordinator) + view.hidden = true + coordinator.animateAlongsideTransition(nil) { _ in + // http://saygoodnight.com/2015/06/18/openpics-swift-rotation.html + if self.navigationController?.visibleViewController != self { + self.view.frame = CGRectMake(self.view.frame.origin.x, self.view.frame.origin.y, size.width, size.height) + } + self.collectionView?.reloadData() + self.scrollCollectionViewToInitialPosition() + self.view.hidden = false + } + } + var isFirstAppearance = true func updateTitle() { title = photoKitAssetList.title } + func scrollCollectionView(to indexPath: NSIndexPath) { + collectionView?.scrollToItemAtIndexPath(indexPath, atScrollPosition: .Bottom, animated: false) + } + func scrollCollectionViewToInitialPosition() { guard isFirstAppearance else { return } - guard photoKitAssetList.count > 0 else { - return - } - let index = NSIndexPath(forRow: self.photoKitAssetList.count - 1, inSection: 0) - collectionView?.scrollToItemAtIndexPath(index, atScrollPosition: .Bottom, animated: false) - + let indexPath = NSIndexPath(forRow: self.photoKitAssetList.count - 1, inSection: 0) + scrollCollectionView(to: indexPath) isFirstAppearance = false } - // MARK: - UICollectionViewDataSource - + // MARK: - UICollectionViewDataSource override func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int { return photoKitAssetList.count } @@ -77,8 +95,7 @@ class AssetListViewController: UICollectionViewController { } override func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell { - guard let cell = collectionView.dequeueReusableCellWithReuseIdentifier("AssetCell", forIndexPath: indexPath) as? AssetCell, - nohanaImagePickerController = nohanaImagePickerController else { + guard let cell = collectionView.dequeueReusableCellWithReuseIdentifier("AssetCell", forIndexPath: indexPath) as? AssetCell else { fatalError("failed to dequeueReusableCellWithIdentifier(\"AssetCell\")") } cell.tag = indexPath.item diff --git a/NohanaImagePicker/MomentViewController.swift b/NohanaImagePicker/MomentViewController.swift index 89c3b3a..c782912 100644 --- a/NohanaImagePicker/MomentViewController.swift +++ b/NohanaImagePicker/MomentViewController.swift @@ -26,18 +26,14 @@ class MomentViewController: AssetListViewController, ActivityIndicatable { guard isFirstAppearance else { return } - guard let collectionView = collectionView else { - return - } let lastSection = momentAlbumList.count - 1 guard lastSection >= 0 else { return } - let index = NSIndexPath(forItem: momentAlbumList[lastSection].count - 1, inSection: lastSection) - collectionView.scrollToItemAtIndexPath(index, atScrollPosition: .Bottom, animated: false) + let indexPath = NSIndexPath(forItem: momentAlbumList[lastSection].count - 1, inSection: lastSection) + scrollCollectionView(to: indexPath) isFirstAppearance = false - } // MARK: - UICollectionViewDataSource @@ -57,8 +53,7 @@ class MomentViewController: AssetListViewController, ActivityIndicatable { // MARK: - UICollectionViewDelegate override func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell { - guard let cell = collectionView.dequeueReusableCellWithReuseIdentifier("AssetCell", forIndexPath: indexPath) as? AssetCell, - nohanaImagePickerController = nohanaImagePickerController else { + guard let cell = collectionView.dequeueReusableCellWithReuseIdentifier("AssetCell", forIndexPath: indexPath) as? AssetCell else { return UICollectionViewCell(frame: CGRectZero) } diff --git a/NohanaImagePicker/NohanaImagePickerController.swift b/NohanaImagePicker/NohanaImagePickerController.swift index 135d805..c95b766 100644 --- a/NohanaImagePicker/NohanaImagePickerController.swift +++ b/NohanaImagePicker/NohanaImagePickerController.swift @@ -100,10 +100,10 @@ public class NohanaImagePickerController: UIViewController { assetCollectionSubtypes: assetCollectionSubtypes, mediaType: mediaType, shouldShowEmptyAlbum: shouldShowEmptyAlbum, - handler: { + handler: { [weak albumListViewController] in dispatch_async(dispatch_get_main_queue(), { () -> Void in - albumListViewController.isLoading = false - albumListViewController.tableView.reloadData() + albumListViewController?.isLoading = false + albumListViewController?.tableView.reloadData() }) }) albumListViewController.nohanaImagePickerController = self diff --git a/NohanaImagePicker/Size.swift b/NohanaImagePicker/Size.swift index ed56025..f1f427b 100644 --- a/NohanaImagePicker/Size.swift +++ b/NohanaImagePicker/Size.swift @@ -8,7 +8,14 @@ struct Size { - static let statusBarHeight = UIApplication.sharedApplication().statusBarFrame.size.height + static var statusBarHeight: CGFloat { + get { + if UIApplication.sharedApplication().statusBarHidden { + return 0 + } + return UIApplication.sharedApplication().statusBarFrame.size.height + } + } static func navigationBarHeight(viewController: UIViewController) -> CGFloat { return viewController.navigationController?.navigationBar.frame.size.height ?? CGFloat(44)