3

I have enabled the keyboardDismissMode to be .Interactive, and I want to change the contentInset of UITextView when keyboard appears, just like the Message App in iOS 7 and 8. However, it causes jump stutters when keyboard disappears.

Here is my code.

func keyboardWillShow(notification: NSNotification) {
    var info: NSDictionary = notification.userInfo!
    var keyboardSize: CGSize = info.objectForKey(UIKeyboardFrameEndUserInfoKey)!.CGRectValue().size

    self.textView.contentInset = UIEdgeInsetsMake(64, 0, keyboardSize.height, 0)
    self.textView.scrollIndicatorInsets = self.textView.contentInset

    // 64 is the UINavigationBar height and 44 is the UIToolBar below. The Toolbar is the inputAccessory
}

func keyboardWillHide(notification: NSNotification) {
    // Change contentInset
    self.textView.contentInset = UIEdgeInsetsMake(64, 0, 44, 0)
    self.textView.scrollIndicatorInsets = self.textView.contentInset
}

enter image description here

enter image description here

I have also noticed that it only happens when the content is short. When it is filled with a long essay, it wouldn't jump with stutter.

I am running this on iOS 8. Any help?

Update:

It appears that its not related to the text length, but an empty line. The jump stutter happen when there is an empty line in the text.

Henry Ngan
  • 572
  • 1
  • 4
  • 24
  • Are you just clicking or are you dragging to make the keyboard disappear? – Lyndsey Scott Dec 24 '14 at 16:31
  • Dragging. That is the only way to dismiss the keyboard. – Henry Ngan Dec 24 '14 at 16:32
  • OK, then looks like your gesture is also dragging the scrollview and since it's bounces property is set to yes, it's causing the "stutter" you've observed. – Lyndsey Scott Dec 24 '14 at 16:33
  • Is the UIPanGestureRecognizer that makes the keyboard disappear actually *on* the "Add Photo" view? – Lyndsey Scott Dec 24 '14 at 16:35
  • Yes. The problem is gone when I disable the bouncing. But I want to keep it. In the GIF, the second bounce is completely normal. But in the first bounce, it would bounce to original place (the place it should be at), then suddenly goes upwards then bounce back to original place, which is very weird. I want to achieve the smoothness like in the Message app. – Henry Ngan Dec 24 '14 at 16:35
  • No. I don't use any GestureRecognizer. I only set the keyboardDismissMode to .Interactive in storyboard. – Henry Ngan Dec 24 '14 at 16:37
  • Even in the Messages app, you'll see the scrollview continue to scroll after you swipe the keyboard down; and if there's only one short message in the message thread, for example, your scrollview will bounce. – Lyndsey Scott Dec 24 '14 at 16:45
  • I know. I want the bounce back to the original place effect. But in my GIF, you can see that the first time I drag it, it bounces back to the original place, then further upward, then back to original place. The second drag is normal though, which is just like the effect in Message app. – Henry Ngan Dec 24 '14 at 16:51
  • I think it has to do with the fact that you're changing the contentInset mid-scroll. I know you're not animating your content inset, but perhaps this question and answer might help: http://stackoverflow.com/q/26050872/2274694 – Lyndsey Scott Dec 24 '14 at 17:02
  • Thanks for your kind help! But I am not familiar with Objective-C. Could you please tell me where should I write that code in? I don't have a `setLoadingScrollViewInsets` method in my code. How should I use the code? – Henry Ngan Dec 24 '14 at 17:16
  • I have had same issue with UITextField in UICollectionView. I resolved it by checking if it's necessary to set contentInset (it's bottom value) in keyboardWillShow. So now it's like this: if(self.collectionView.contentInset.bottom != 0) self.collectionView.contentInset = UIEdgeInsetsZero; – Nick Rostov Oct 16 '15 at 02:56

0 Answers0