SultanSh
SultanSh

Reputation: 103

How to create that moveable UItextfield?

How to create that moveable UItextfield that stay in the bottom of the screen and when the keyboard appears it moves to the top of the keyboared in iphone applications? How to do it in Xcode 4.2? Just like in whatsapp and Skype chat. I want to use to input string to a table.

Upvotes: 0

Views: 367

Answers (2)

Jack Humphries
Jack Humphries

Reputation: 13267

You can use notifications as Hubert mentioned, or you can constantly check to see if the UITextField is the first responder. If it is the first responder, this means that the text field is selected and the one that the keyboard will affect.

-(void)viewDidLoad {

    NSTimer *timer = [NSTimer scheduledTimerWithTimeInterval:0.01 target:self selector:@selector(checkTextField) userInfo:nil repeats:YES];

}

-(void)checkTextField {

    if (textField isFirstResponder) {

        //the text field has been tapped and the keyboard will come up, so animate the text field moving up here


    } else {

        //the text field is not selected, so it should be in its original position

    }

}

Upvotes: 0

Hubert Kunnemeyer
Hubert Kunnemeyer

Reputation: 2261

You should register the viewController as a listener for Keyboard Notifications. When the keyboard appears a notification is fired with a user dictionary. The dictionary will contain useful information such as keyboard positions relative to the screen to use to animate your textFields frame to a new position. Check out the documents:

http://developer.apple.com/library/ios/#DOCUMENTATION/StringsTextFonts/Conceptual/TextAndWebiPhoneOS/KeyboardManagement/KeyboardManagement.html

Specifically the notifications you want are:

[[NSNotificationCenter defaultCenter] addObserver:self
            selector:@selector(keyboardWasShown:)
            name:UIKeyboardDidShowNotification object:nil];

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

You'll want to add these in the viewDidLoad in most cases. And don't forget to unregister for the notifications(removeObserver:) later when you done.Such as in the viewDidUnload.

Upvotes: 1

Related Questions