IPhone - How can I scroll a UITextField and resize a UITextView when a keybord appears

I have a UIScrollView inside which I have a UITextField and a UITextView. I want the keyboard not to hide the field we're working on. If my user clicks on a TextField, I want it to scroll to the top of the screen. If my user touches the TextView, I want him to resize to the empty space left at the top of the keyboard. My keyboard has an additional view (toolbar).

I am currently catching a UIKeyboardWillShowNotification notification, and I am using this code, which actually does not work (the text image has been changed, but is still behind the keyboard): This code worked well when I did not have a UIScrollView.

- (void)keyboardWillShow:(NSNotification *)notification {

    if ([self.noteView isFirstResponder] == NO) return;

    /*
     Reduce the size of the text view so that it not obscured by the keyboard.
     Animate the resize so that it in sync with the appearance of the keyboard.
     */

    NSDictionary *userInfo = [notification userInfo];
    NSValue* aValue = [userInfo objectForKey:UIKeyboardFrameEndUserInfoKey];                            // Get the origin of the keyboard when it displayed.
    NSValue *animationDurationValue = [userInfo objectForKey:UIKeyboardAnimationDurationUserInfoKey];   // Get the duration of the animation.

    // Get the top of the keyboard as the y coordinate of its origin in self view coordinate system. The bottom of the text view frame should align with the top of the keyboard final position.
    CGRect keyboardRect = [aValue CGRectValue];
    keyboardRect = [self.view convertRect:keyboardRect fromView:nil];

    CGFloat keyboardTop = keyboardRect.origin.y;
    CGRect newTextViewFrame = self.view.bounds;
    newTextViewFrame.size.height = keyboardTop - self.view.frame.origin.y; // Using bounds here does not help

    NSTimeInterval animationDuration;
    [animationDurationValue getValue:&animationDuration];

    // Animate the resize of the text view frame in sync with the keyboard appearance.
    [UIView beginAnimations:nil context:NULL];
    [UIView setAnimationDuration:animationDuration];

     self.noteView.frame = newTextViewFrame;

    [UIView commitAnimations];
}

:

  • ?

  • Textview ?

  • , TextField ?

+3
1

uiscrollview, :

- (void)scrollRectToVisible:(CGRect)rect animated:(BOOL)animated

, .

0

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


All Articles