0

I would like to resize a UITextView when the keyboard shows up but I can't seem to do this. I've created a single view with an UITextView in it. In code, I would like to manually resize the height of this text view. I do this:

_textView.translatesAutoresizingMaskIntoConstraints  = NO;

NSLayoutConstraint *constraint = [NSLayoutConstraint constraintWithItem:_textView attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutAttributeNotAnAttribute multiplier:1.0f constant:216.0f];
[_textView addConstraint:constraint];

In Interface Builder, I say that the margin between the text view and the superview can be greater than or equal to 0.

When I run the app, it gives the error that constrains can't be satisfied simultanously:

"<NSLayoutConstraint:0x886e550 V:[UITextView:0x7b0fa00(216)]>",
"<NSLayoutConstraint:0x886f7c0 UITextView:0x7b0fa00.bottom == UIView:0x886e3c0.bottom>",
"<NSLayoutConstraint:0x886f700 V:|-(0)-[UITextView:0x7b0fa00]   (Names: '|':UIView:0x886e3c0 )>",
"<NSAutoresizingMaskLayoutConstraint:0x71a2d10 h=--- v=--- V:[UIWindow:0x8a792f0(480)]>",
"<NSAutoresizingMaskLayoutConstraint:0x71a1490 h=-&- v=-&- UIView:0x886e3c0.height == UIWindow:0x8a792f0.height - 20>"


Will attempt to recover by breaking constraint 
<NSLayoutConstraint:0x886e550 V:[UITextView:0x7b0fa00(216)]>

I'm not sure on how to make sure that all constraints are satisfied. Could anyone give me a hand in this?

Thanks!

Master Stroke
  • 5,108
  • 2
  • 26
  • 57
Devos50
  • 2,025
  • 4
  • 25
  • 56
  • Does this help? http://stackoverflow.com/questions/14105420/animating-text-box-when-clicked/14107365#14107365 I make my View Controller's view.frame shorter when the keyboard appears and taller when the keyboard disappears, similar to how the Messages app behaves. – John Sauer Jan 03 '13 at 14:42

2 Answers2

8

With iOS7, it may be better to set UIEdgeInsets instead of resizing the view since we are sometimes dealing with translucence. In some applications we still want the text to be seen scrolling through a toolbar, keyboard, or other UI overlay. Another benefit is that animation is not typically needed with a change of the content inset as that happens "behind the scenes."

Apple provides a very easy and elegant solution in their developer resources, although it is somewhat buried and hard to find. Here is a link:

https://developer.apple.com/library/ios/documentation/StringsTextFonts/Conceptual/TextAndWebiPhoneOS/KeyboardManagement/KeyboardManagement.html

Go to Listing 5-1 for a complete code snippet.

TMilligan
  • 3,520
  • 1
  • 18
  • 12
  • Since iOS7, this should be the accepted answer. Just be aware that the view you want to "resize" may not have equals `contentInset` and `scrollIndicatorInsets`, so maybe you should make copy of them in `keyboardWasShown` and, instead of setting them to `UIEdgeInsetsZero`, restore then in `keyboardWillBeHidden`. – Pierre-David Belanger Oct 24 '14 at 15:20
  • I've just tried this with iOS9.3, and it seems to play well with autolayout. – Chris Prince Jun 05 '16 at 16:05
-1

Possible duplicate of

How to resize UITextView on iOS when a keyboard appears? ?

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillShow:) 
                                             name:UIKeyboardWillShowNotification object:self.view.window]; 

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillHide:) 
                                             name:UIKeyboardWillHideNotification object:self.view.window]; 

- (void)keyboardWillShow:(NSNotification *)notif
{
[thetextView setFrame:CGRectMake(20, 49, 280, 187)]; //Or where ever you want the view to go


}

- (void)keyboardWillHide:(NSNotification *)notif
{
[thetextView setFrame:CGRectMake(20, 49, 280, 324)]; //return it to its original position

}
Community
  • 1
  • 1
Master Stroke
  • 5,108
  • 2
  • 26
  • 57