IOS: How to adjust the height of the area covered by the keyboard in iOS

I am interested to know about the function in iOS. Please help me if you can.

Scenario: I use a text box in which the name is entered. Its in the bottom half of the screen. Below the text box is a label that displays the number of characters remaining (for example, on Twitter).

Problem: when I put a text box in the upper half of the screen. both the text field and the label are displayed. But when I put them in the lower half, the apple keyboard covers part of the label. Is there a way that I control the area covered so that the label below is also visible? I hope I made myself clear enough. Thanks.

+4
source share
4 answers

Here I used the delegate method for UITextView The same thing you can do for UITextField

-In this code, when the user starts to enter values ​​in a textview , he makes your look smaller than his original with animation

-When the user finishes entering the values, the size of the original size will be displayed.

 -(void)textViewDidBeginEditing:(UITextView *)textView { CGRect frame = self.view.frame; frame.origin.y = -100; [self.view setFrame:frame]; [UIView commitAnimations]; } -(void)textViewDidEndEditing:(UITextView *)textView { CGRect frame = self.view.frame; frame.origin.y = 0; [self.view setFrame:frame]; [UIView commitAnimations]; } 

If you want to know about delegates, the link will help you.

+5
source

In this case, you need to move the text box when the keyboard appears. You can register a notification that will be known when the keyboard appears, and scrolling to scroll all the content up the screen can do the job for you

See this question, it explains well how to manage something like this

+2
source

AFAIK You cannot control the size of your own iOS keyboard, all you can and should do is make them scroll through and scroll up.

So, common practice goes something like this.

  • Sign up for a keyboard notification. UIKeyboardWillShowNotification
  • In the method that the notification listener will be called, set the appropriate size of the scroll content and set the content offset.

     self.scrollView.contentSize = CGSizeMake(320, 267); self.scrollView.contentInset = UIEdgeInsetsMake(0, 0, localKeyboardFrame.size.height, 0); [self.scrollView scrollRectToVisible:<rect of view you want to scroll to> animated:YES]; 
  • Discard changes when hiding the keyboard using the corresponding notification. UIKeyboardWillHideNotification

     self.scrollView.contentInset = UIEdgeInsetsZero 

And here is a description of the manual for the iOS interface .

+2
source

Add a method to your viewDidLoad

following:
 [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(showKeyboard) name:UIKeyboardWillShowNotification object:nil]; [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(hideKeyboard) name:UIKeyboardWillHideNotification object:nil]; 

And after that ... Declare the following 2 methods in your .m file

 -(void)showKeyboard { [UIView beginAnimations:@"slide" context:nil]; [UIView setAnimationDuration:0.3]; self.view.transform = CGAffineTransformMakeTranslation(0, -100); [UIView commitAnimations]; } -(void)hideKeyboard { [UIView beginAnimations:@"slide" context:nil]; [UIView setAnimationDuration:0.1]; self.view.transform = CGAffineTransformMakeTranslation(0, 0); [UIView commitAnimations]; } 
+1
source

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


All Articles