-2

My app has a view controller with eight TextView boxes. I have them all in a content view, inside of a ScrollView. I've adjusted the content and scroll height so I can scroll through and see all of the TextViews, but when the keyboard comes up, it covers the bottom TextView and I can't scroll far enough for that TextView to actually be visible.

All the solutions I've tried have only partially worked. Currently I have this one in my code, and it does allow the bottom TextView to be visible, but now the top two can't be reached when I scroll with the keyboard open.

Most other solutions are for TextFields (like this one), which I have tried by essentially replacing all the TextField references with TextView, but that doesn't work.

The only thing I can actually get to work is if I drastically increase the content height to allow for scrolling further. But I don't want a bunch of empty space at the bottom of the screen.

How can I make sure all TextViews can be scrolled to with the keyboard open?

Community
  • 1
  • 1
  • I personally use https://github.com/michaeltyson/TPKeyboardAvoiding - set your ScrollView's class to the provided one and it handles the rest. – sschale Mar 14 '17 at 03:37
  • But would I be able to use something like that if I plan to submit my app to the App Store? I've read that Apple's guidelines are strict, so I've been trying to do all the coding myself without relying on drop-in solutions that I don't personally own the rights to. – unchainedprey Mar 14 '17 at 03:42
  • That won't be a problem at all. Almost every piece of software is going to have dependencies of some sort, you just have to read the licenses of any item you include. – sschale Mar 14 '17 at 04:15

1 Answers1

0

You should try below solution

      #define k_KEYBOARD_OFFSET 95.0

    -(void)keyboardWillAppear {
        // Move current view up / down with Animation
        if (self.view.frame.origin.y >= 0)
        {
            [self moveViewUp:NO];
        }
        else if (self.view.frame.origin.y < 0)
        {
            [self moveViewUp:YES];
        }
    }

    -(void)keyboardWillDisappear {
        if (self.view.frame.origin.y >= 0)
        {
            [self moveViewUp:YES];
        }
        else if (self.view.frame.origin.y < 0)
        {
            [self moveViewUp:NO];
        }
    }

- (void)textViewDidBeginEditing:(UITextView *)textView1 {
    {
            //move the main view up, so the keyboard will not hide it.
            if  (self.view.frame.origin.y >= 0)
            {
                [self moveViewUp:YES];
            }
    }

    //Custom method to move the view up/down whenever the keyboard is appeared / disappeared
    -(void)moveViewUp:(BOOL)bMovedUp
    {
        [UIView beginAnimations:nil context:NULL];
        [UIView setAnimationDuration:0.4]; // to slide the view up

        CGRect rect = self.view.frame;
        if (bMovedUp) {
            // 1. move the origin of view up so that the text field will come above the keyboard
            rect.origin.y -= k_KEYBOARD_OFFSET;

            // 2. increase the height of the view to cover up the area behind the keyboard
            rect.size.height += k_KEYBOARD_OFFSET;
        } else {
            // revert to normal state of the view.
            rect.origin.y += k_KEYBOARD_OFFSET;
            rect.size.height -= k_KEYBOARD_OFFSET;
        }

        self.view.frame = rect;

        [UIView commitAnimations];
    }

    - (void)viewWillAppear:(BOOL)animated
    {
        // register keyboard notifications to appear / disappear the keyboard
        [[NSNotificationCenter defaultCenter] addObserver:self
                                                 selector:@selector(keyboardWillAppear)
                                                     name:UIKeyboardWillShowNotification
                                                   object:nil];

        [[NSNotificationCenter defaultCenter] addObserver:self
                                                 selector:@selector(keyboardWillDisappear)
                                                     name:UIKeyboardWillHideNotification
                                                   object:nil];
    }

    - (void)viewWillDisappear:(BOOL)animated
    {
        // unregister for keyboard notifications while moving to the other screen.
        [[NSNotificationCenter defaultCenter] removeObserver:self
                                                        name:UIKeyboardWillShowNotification
                                                      object:nil];

        [[NSNotificationCenter defaultCenter] removeObserver:self
                                                        name:UIKeyboardWillHideNotification
                                                      object:nil];
    }
Pradumna Patil
  • 2,180
  • 3
  • 17
  • 46