How to make a UITextField move up when keyboard is present

Question!

With the iPhone SDK:

I have a UIView with UITextFields that brings up a keyboard. I need it to be able to:

  1. Allow scrolling of the contents of the UIScrollView to see the other text fields once the keyboard is brought up

  2. Automatically "jump" (by scrolling up) or shortening

I know that I need a UIScrollView. I've tried changing the class of my UIView to a UIScrollView but I'm still unable to scroll the textboxes up or down.

Do I need both a UIView and a UIScrollView? Does one go inside the other? [EDIT: I now know that you want a UIView inside of a UIScrollView, and the trick is to programatically set the content size of the UIScrollView to the frame size of the UIView.]

Then what needs to be implemented in order to automatically scroll to the active text field?

Ideally as much of the setup of the components as possible will be done in Interface Builder. I'd like to only write code for what needs it.

Note: the UIView (or UIScrollView) that I'm working with is brought up by a tabbar (UITabBar), which needs to function as normal.


Edit: I am adding the scroll bar just for when the keyboard comes up. Even though it's not needed, I feel like it provides a better interface because then the user can scroll and change textboxes, for example.

I've got it working where I change the frame size of the UIScrollView when the keyboard goes up and down. I'm simply using:

-(void)textFieldDidBeginEditing:(UITextField *)textField { 
    //Keyboard becomes visible
    scrollView.frame = CGRectMake(scrollView.frame.origin.x, 
                     scrollView.frame.origin.y, 
scrollView.frame.size.width,
scrollView.frame.size.height - 215 + 50);   //resize
}

-(void)textFieldDidEndEditing:(UITextField *)textField {
   //keyboard will hide
    scrollView.frame = CGRectMake(scrollView.frame.origin.x, 
       scrollView.frame.origin.y, 
     scrollView.frame.size.width,
      scrollView.frame.size.height + 215 - 50); //resize
}

However this doesn't automatically "move up" or center the lower text fields in the visible area, which is what I would really like.

By : philfreo


Answers

first of all, u better design ur page as no need to scroll ur view. if u have many textfields, u still dont have to use scrollview, it just make things complicated. u can just add a container uiview on the top of controller's original view then you put those textfields on this view. when keyboard shows of disappears, just move this container view with an animation.

By : Bruce Lee


IF YOU ARE STILL STRUGGLING WITH THIS — READ MY POST

I have come up with a solution today. I've read numerous posts and "tutorials" about this issue and NONE of them works in every case (most of them are copy-pastes of each other). Even Apple's officially proposed "solution" doesn't work, and more, it totally doesn't work in landscape mode. Shame on Apple for not giving the devs means to beat such a common basic issue. Very unprofessional. Such an amazing framework (Cocoa) and such a nasty underrated issue.

Now, my solution: make UIScrollView your root view and then put everything in it. Then subclass your view controller from this KeyboardAwareController class (you may want to redefine scrollView and keyboardPadding methods):

// // KeyboardAwareController.h // Sociopathy // // Created by Admin on 13.01.14. // Copyright (c) 2014 kuchumovn. All rights reserved. //

#import 


Try this it's working perfectly:

if Firstnametxt.text == "" || Passwordtxt.text == "" || emailtxt.text == ""
    {
        if Firstnametxt.text == ""
        {
            Firstnametxt!.shake(10, withDelta: 5, speed: 0.05, shakeDirection: ShakeDirection.Horizontal)
            Firstnametxt.becomeFirstResponder()
        }
        else if Passwordtxt.text == ""
        {
            Passwordtxt!.shake(10, withDelta: 5, speed: 0.05, shakeDirection: ShakeDirection.Horizontal)
            Passwordtxt.becomeFirstResponder()
        }
        else if emailtxt.text == ""

        {

            emailtxt!.shake(10, withDelta: 5, speed: 0.05, shakeDirection: ShakeDirection.Horizontal)
            emailtxt.becomeFirstResponder()
        }

    }
    else
    {
        let isValidEmail:Bool = self.isValidEmail(emailtxt.text!)
        if isValidEmail == true
        {
                        }
        else
        {
            emailtxt!.shake(10, withDelta: 5, speed: 0.05, shakeDirection: ShakeDirection.Horizontal)
            emailtxt.becomeFirstResponder()

        }

    }


This video can help you solving your question :)
By: admin