UITextView insert behavior of iOS 7 vs iOS 8

I have a UITextView that behaves fine in iOS 7. In iOS 8, it behaves strangely.

When I enter text that becomes two lines, I update the size of the inputView that contains the text view. I am also updating the size of the text box, but somehow it seems like it scrolls the first line; but this is not reflected in its size.

Video for behavior in iOS 7 (correct): https://www.dropbox.com/s/rr4pea6wudobmk8/iOS7.mov

Video for behavior in iOS 8: https://www.dropbox.com/s/iyqrsp41uz5mn85/iOS8.mov

I set [self setAutomaticallyAdjustsScrollViewInsets:NO]; but no change. This did not help me: What causes this unwanted insertion of content from a UITextView in iOS 8 (not in iOS 7)?

There are results from my NSLog in iOS8:

 2014-12-22 02:02:48.104 Date For Date[68577:10129965] DID BEGIN: textContainerInset Top: 8.000000 - contentInset Bottom: 0.000000 - scrollIndicatorInset: 10.000000 2014-12-22 02:02:50.579 Date For Date[68577:10129965] DID CHANGE: textContainerInset Top: 8.000000 - contentInset Bottom: 0.000000 - scrollIndicatorInset: 10.000000 2014-12-22 02:02:50.768 Date For Date[68577:10129965] DID CHANGE: textContainerInset Top: 8.000000 - contentInset Bottom: 0.000000 - scrollIndicatorInset: 10.000000 2014-12-22 02:02:50.960 Date For Date[68577:10129965] DID CHANGE: textContainerInset Top: 8.000000 - contentInset Bottom: 0.000000 - scrollIndicatorInset: 10.000000 2014-12-22 02:02:51.168 Date For Date[68577:10129965] DID CHANGE: textContainerInset Top: 8.000000 - contentInset Bottom: 0.000000 - scrollIndicatorInset: 10.000000 2014-12-22 02:02:52.145 Date For Date[68577:10129965] DID CHANGE: textContainerInset Top: 8.000000 - contentInset Bottom: 0.000000 - scrollIndicatorInset: 10.000000 2014-12-22 02:02:52.408 Date For Date[68577:10129965] DID CHANGE: textContainerInset Top: 8.000000 - contentInset Bottom: 0.000000 - scrollIndicatorInset: 10.000000 

This is my code:

 - (void)textViewDidBeginEditing:(UITextView *)textView { [textView becomeFirstResponder]; if(!self.previousTextViewContentHeight) self.previousTextViewContentHeight = textView.contentSize.height; [self _scrollToBottom:YES]; NSLog(@"DID BEGIN: textContainerInset Top: %f - contentInset Bottom: %f - scrollIndicatorInset: %f", textView.textContainerInset.top, textView.contentInset.top, textView.scrollIndicatorInsets.top); } - (void)textViewDidEndEditing:(UITextView *)textView { [textView resignFirstResponder]; } - (void)textViewDidChange:(UITextView *)textView { // Textfield NSLog(@"DID CHANGE: textContainerInset Top: %f - contentInset Bottom: %f - scrollIndicatorInset: %f", textView.textContainerInset.top, textView.contentInset.top, textView.scrollIndicatorInsets.top); CGFloat maxHeight = [JSMessageInputView maxHeight]; CGFloat textViewContentHeight = MIN(textView.contentSize.height, maxHeight); CGFloat changeInHeight = textViewContentHeight - self.previousTextViewContentHeight; self.previousTextViewContentHeight = MIN(textViewContentHeight, maxHeight); if(changeInHeight != 0.0f) { textView.frame = CGRectMake(6, 3, textView.frame.size.width, textView.frame.size.height + changeInHeight); textView.contentInset = UIEdgeInsetsMake(0.0f, 0.0f, 0.0f, 0.0f); [textView setContentSize:CGSizeMake(textView.contentSize.width, textView.contentSize.height + changeInHeight)]; // Change table UIEdgeInsets insets = UIEdgeInsetsMake(0, 0.0f, self.bubbleTable.contentInset.bottom + changeInHeight, 0.0f); self.bubbleTable.contentInset = insets; self.bubbleTable.scrollIndicatorInsets = insets; [self _scrollToBottom:YES]; // Change input view CGRect inputViewFrame = self.inputView.frame; self.inputView.frame = CGRectMake(0.0f, inputViewFrame.origin.y - changeInHeight, inputViewFrame.size.width, inputViewFrame.size.height + changeInHeight); } self.inputView.sendButton.enabled = ([textView.text trimWhitespace].length > 0); } 

What am I doing wrong?

+7
ios objective-c uitextview
source share
2 answers

Why aren't you using the UITextView sizeThatFits function? It will automatically calculate the correct height with all the additions:

 CGFloat textViewHeight = [textView sizeThatFits:CGSizeMake(textView.frame.size.width, MAXFLOAT)].height; 
+3
source share

Try textView.textContainer.lineFragmentPadding = 0; in addition to textView.textContainerInset = UIEdgeInsetsZero; . This makes my text hug the borders of a UITextView.

+1
source share

All Articles