Resize UITextView when opening keyboard with auto-layout

I want to resize a UITextView when the keyboard appears, but I cannot do this. I created a single view with a UITextView in it. In the code, I would like to manually resize this text view. I'm doing it:

_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 field between the text view and the superview can be greater than or equal to 0.

When I run the application, it gives an error that the restrictions cannot be satisfied at the same time:

 "<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 am not sure how to make sure that all restrictions are met. Can anyone give me a hand on this?

Thanks!

0
source share
2 answers

Possible duplicate

How to resize UITextView on iOS when 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 } 
-1
source

With iOS7, it might be better to set UIEdgeInsets instead of resizing the view, as we do some translucency sometimes. In some applications, we still want the text to be viewed by scrolling through the toolbar, keyboard, or other overlay of the user interface. Another advantage is that animation is usually not required with changing the insertion of content, as this happens behind the scenes.

Apple provides a very light and elegant solution in its developer resources, although it is somewhat buried and difficult to find. Here's a link:

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

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

+8
source

Source: https://habr.com/ru/post/973447/


All Articles