Moving Textfield when Keyboard is up & related issues

I understand that the issue I’m posting has been discussed a lot in different forums but as far as I have searched in SO and other places, I couldn’t find a solution specific to my issue. I’m posting this to get some inputs.

Note: I have already gone through the ScrollViewSchool videos for Keyboard Insets and related topics elsewhere but I do not have a solution for my issue. Also, I have gone through some of the libraries for Keyboard Handling to understand how its handled. But those didn’t help my case. Appreciate if I can get some directions on how to approach this issue. Thanks in advance.

Problem:

View hierarchy: View → ScrollView → View → Textfield

I have implemented two screens with multiple textfields laid on top of a scrollview to avoid keyboard obscuring the textfield when its at the bottom of the page. I have achieved this using the sample code provided by Apple with minor modifications.

Subscribe to UIKeyboardWillShowNotification and UIKeyboardWillHideNotification
When keyboard is shown, get the height of the keyboard from userInfo dictionary and set appropriate content inset to move the textfield above the Keyboard
When keyboard is hidden, set the content inset to UIEdgeInsetZero to bring back to normal size
The above implementation works perfectly fine when there are more textfields that could occupy the entire screen (assume there are 10 textfields and they extend beyond the frame of the the scroll view). Whenever I tap on a textfield, it moves above the keyboard as expected and I can also scroll the page till the bottom most textfield is visible above the keyboard frame (when the keyboard is still up).

My problem arises when I have only two textfield in the scrollview that are centered vertically in the screen. When I tap on a textfield, the scrollview get an inset equivalent to the keyboard height and it moves above the keyboard as expected but when I scroll the screen, I could see a huge blank space (due to the additional inset) below the textfields.

How can I avoid that blank space when there are only one or two textfields in the page? Should I have to write a different logic to handle that scenario? Any help would be appreciated.

Below is the code:

    override func viewDidLoad() {
        super.viewDidLoad()

        NSNotificationCenter.defaultCenter().addObserver(self, selector: "keyboardWillShow:", name: UIKeyboardWillShowNotification, object: nil)
        NSNotificationCenter.defaultCenter().addObserver(self, selector: "keyboardWillHide:", name: UIKeyboardWillHideNotification, object: nil)
    }


    func keyboardWillShow(notification: NSNotification) {
        if let userInfo = notification.userInfo {
            let keyboardFrame = (userInfo[UIKeyboardFrameEndUserInfoKey] as? NSValue)?.CGRectValue()
            if let kbFrame = keyboardFrame {

                let inset = CGRectGetHeight(kbFrame)
                scrollView.contentInset = UIEdgeInsetsMake(0, 0, inset, 0)
                scrollView.scrollIndicatorInsets = UIEdgeInsetsMake(0, 0, inset, 0)
            }
        }
    }

    func keyboardWillHide(notfication: NSNotification) {
        scrollView.contentInset = UIEdgeInsetsZero
        scrollView.scrollIndicatorInsets = UIEdgeInsetsZero
    }

Hello Ramkumar ,

Just take a fixed height something like these " #define kOFFSET_FOR_KEYBOARD 50.0" and move your scroll bar in textfiled delegate method .

and try these code . Hope it works .

#pragma UIKeyboardWillShow and UIKeyboardWillHide

-(void)keyboardWillShow {

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

}

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

#pragma UIKeyboar MoveUp and MoveDown

-(void)setViewMovedUp:(BOOL)movedUp
{
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:0.3];

CGRect rect = self.view.frame;
if (movedUp)
{
    
    rect.origin.y -= kOFFSET_FOR_KEYBOARD;
    rect.size.height += kOFFSET_FOR_KEYBOARD;
}

self.view.frame = rect;

[UIView commitAnimations];

}

-(void)viewToNormalPosition
{
CGRect rect = self.view.frame;

rect.origin.y += kOFFSET_FOR_KEYBOARD;
rect.size.height -= kOFFSET_FOR_KEYBOARD;
self.view.frame = rect;

}