From 3da503d47a47cc7aed2e5b1929ca37d460902c02 Mon Sep 17 00:00:00 2001 From: nixzhu Date: Tue, 24 Mar 2015 15:06:57 +0800 Subject: [PATCH] updateUIWithKeyboardChange, conversationCollectionView.contentInset properly --- .../ConversationViewController.swift | 34 ++++++++++++++----- 1 file changed, 26 insertions(+), 8 deletions(-) diff --git a/Yep/ViewControllers/Conversation/ConversationViewController.swift b/Yep/ViewControllers/Conversation/ConversationViewController.swift index 7c9fc3b4..b4192e6c 100644 --- a/Yep/ViewControllers/Conversation/ConversationViewController.swift +++ b/Yep/ViewControllers/Conversation/ConversationViewController.swift @@ -25,7 +25,7 @@ class ConversationViewController: UIViewController { // 使 messageToolbar 随着键盘出现或消失而移动 - var updateMessageToolbarWithKeyboardChange = false { + var updateUIWithKeyboardChange = false { willSet { keyboardChangeObserver = newValue ? NSNotificationCenter.defaultCenter() : nil } @@ -42,7 +42,7 @@ class ConversationViewController: UIViewController { deinit { - updateMessageToolbarWithKeyboardChange = false + updateUIWithKeyboardChange = false } override func viewDidLoad() { @@ -51,15 +51,24 @@ class ConversationViewController: UIViewController { conversationCollectionView.registerNib(UINib(nibName: chatLeftTextCellIdentifier, bundle: nil), forCellWithReuseIdentifier: chatLeftTextCellIdentifier) conversationCollectionView.registerNib(UINib(nibName: chatRightTextCellIdentifier, bundle: nil), forCellWithReuseIdentifier: chatRightTextCellIdentifier) - var contentInset = conversationCollectionView.contentInset - contentInset.bottom = messageToolbar.intrinsicContentSize().height - conversationCollectionView.contentInset = contentInset + setConversaitonCollectionViewOriginalContentInset() messageToolbarBottomConstraint.constant = 0 - updateMessageToolbarWithKeyboardChange = true + updateUIWithKeyboardChange = true } + // MARK: Private + + private func setConversaitonCollectionViewOriginalContentInsetBottom(bottom: CGFloat) { + var contentInset = conversationCollectionView.contentInset + contentInset.bottom = bottom + conversationCollectionView.contentInset = contentInset + } + + private func setConversaitonCollectionViewOriginalContentInset() { + setConversaitonCollectionViewOriginalContentInsetBottom(messageToolbar.intrinsicContentSize().height) + } // MARK: Keyboard @@ -74,7 +83,9 @@ class ConversationViewController: UIViewController { UIView.animateWithDuration(animationDuration, delay: 0, options: UIViewAnimationOptions(animationCurveValue), animations: { () -> Void in self.messageToolbarBottomConstraint.constant = keyboardHeight self.view.layoutIfNeeded() - }, completion: { (finished) -> Void in + + }, completion: { (finished) -> Void in + self.setConversaitonCollectionViewOriginalContentInsetBottom(keyboardHeight + self.messageToolbar.intrinsicContentSize().height) }) } } @@ -83,10 +94,13 @@ class ConversationViewController: UIViewController { if let userInfo = notification.userInfo { let animationDuration: NSTimeInterval = (userInfo[UIKeyboardAnimationDurationUserInfoKey] as! NSNumber).doubleValue + self.setConversaitonCollectionViewOriginalContentInset() + UIView.animateWithDuration(animationDuration, delay: 0, options: .BeginFromCurrentState, animations: { () -> Void in self.messageToolbarBottomConstraint.constant = 0 self.view.layoutIfNeeded() - }, completion: { (finished) -> Void in + + }, completion: { (finished) -> Void in }) } } @@ -126,4 +140,8 @@ extension ConversationViewController: UICollectionViewDataSource, UICollectionVi return CGSizeMake(collectionViewWidth, 60) } + + func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath) { + view.endEditing(true) + } } \ No newline at end of file