From 7432d8182e9bedd76bd75734522cfe3dec2d2fce Mon Sep 17 00:00:00 2001 From: Diego Sanchez Date: Wed, 30 Mar 2016 11:29:49 +0100 Subject: [PATCH] Adjust insets and offset when top layout guide changes --- .../BaseChatViewController.swift | 31 ++++++++++++------- .../ChatController/KeyboardTracker.swift | 2 +- 2 files changed, 20 insertions(+), 13 deletions(-) diff --git a/Chatto/Source/ChatController/BaseChatViewController.swift b/Chatto/Source/ChatController/BaseChatViewController.swift index 70dac63..5f2dbe2 100644 --- a/Chatto/Source/ChatController/BaseChatViewController.swift +++ b/Chatto/Source/ChatController/BaseChatViewController.swift @@ -118,6 +118,8 @@ public class BaseChatViewController: UIViewController, UICollectionViewDataSourc self.presenterFactory = self.createPresenterFactory() self.presenterFactory.configure(withCollectionView: self.collectionView) + + self.automaticallyAdjustsScrollViewInsets = false } private var inputContainerBottomConstraint: NSLayoutConstraint! @@ -169,15 +171,28 @@ public class BaseChatViewController: UIViewController, UICollectionViewDataSourc let inputHeightWithKeyboard = self.view.bounds.height - self.inputContainer.frame.minY let newInsetBottom = self.constants.defaultContentInsets.bottom + inputHeightWithKeyboard let insetBottomDiff = newInsetBottom - self.collectionView.contentInset.bottom + let newInsetTop = self.topLayoutGuide.length + self.constants.defaultContentInsets.top + let insetTopDiff = newInsetTop - self.collectionView.contentInset.top let contentSize = self.collectionView.collectionViewLayout.collectionViewContentSize() let allContentFits = self.collectionView.bounds.height - newInsetBottom - (contentSize.height + self.collectionView.contentInset.top) >= 0 - let currentDistanceToBottomInset = max(0, self.collectionView.bounds.height - self.collectionView.contentInset.bottom - (contentSize.height - self.collectionView.contentOffset.y)) - let newContentOffsetY = self.collectionView.contentOffset.y + insetBottomDiff - currentDistanceToBottomInset + let newContentOffsetY = max(-newInsetTop, self.collectionView.contentOffset.y + insetBottomDiff - insetTopDiff) + + self.collectionView.contentInset = { + var currentInsets = self.collectionView.contentInset + currentInsets.bottom = newInsetBottom + currentInsets.top = newInsetTop + return currentInsets + }() + + self.collectionView.scrollIndicatorInsets = { + var currentInsets = self.collectionView.scrollIndicatorInsets + currentInsets.bottom = self.constants.defaultScrollIndicatorInsets.bottom + inputHeightWithKeyboard + currentInsets.top = self.topLayoutGuide.length + self.constants.defaultScrollIndicatorInsets.top + return currentInsets + }() - self.collectionView.contentInset.bottom = newInsetBottom - self.collectionView.scrollIndicatorInsets.bottom = self.constants.defaultScrollIndicatorInsets.bottom + inputHeightWithKeyboard let inputIsAtBottom = self.view.bounds.maxY - self.inputContainer.frame.maxY <= 0 if allContentFits { @@ -185,14 +200,6 @@ public class BaseChatViewController: UIViewController, UICollectionViewDataSourc } else if !isInteracting || inputIsAtBottom { self.collectionView.contentOffset.y = newContentOffsetY } - - self.workaroundContentInsetBugiOS_9_0_x() - } - - func workaroundContentInsetBugiOS_9_0_x() { - // Fix for http://www.openradar.me/22106545 - self.collectionView.contentInset.top = self.topLayoutGuide.length + self.constants.defaultContentInsets.top - self.collectionView.scrollIndicatorInsets.top = self.topLayoutGuide.length + self.constants.defaultScrollIndicatorInsets.top } func rectAtIndexPath(indexPath: NSIndexPath?) -> CGRect? { diff --git a/Chatto/Source/ChatController/KeyboardTracker.swift b/Chatto/Source/ChatController/KeyboardTracker.swift index c8a6c47..e12adf1 100644 --- a/Chatto/Source/ChatController/KeyboardTracker.swift +++ b/Chatto/Source/ChatController/KeyboardTracker.swift @@ -122,7 +122,7 @@ class KeyboardTracker { private func bottomConstraintFromTrackingView() -> CGFloat { let trackingViewRect = self.view.convertRect(self.keyboardTrackerView.bounds, fromView: self.keyboardTrackerView) - return self.view.bounds.height - trackingViewRect.maxY + return max(0, self.view.bounds.height - trackingViewRect.maxY) } func layoutTrackingViewIfNeeded() {