Adjust insets and offset when top layout guide changes

This commit is contained in:
Diego Sanchez 2016-03-30 11:29:49 +01:00
parent a530b7f398
commit 7432d8182e
2 changed files with 20 additions and 13 deletions

View File

@ -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? {

View File

@ -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() {