Hi can someone help me create this feature, I would like to have a text box at the bottom of my app, and as soon as the user clicks on it, it should raise that text box to the middle of screen right above the keyboard. Similar to skype app, thanks!
Asked
Active
Viewed 284 times
0

sebi
- 815
- 7
- 15
- 26
-
check this question http://stackoverflow.com/questions/9453807/uitextfield-to-be-displayed-on-top-of-keyboard-beginner – Omar Abdelhafith Jul 03 '12 at 22:31
-
Use a UIScrollView. Then respond to the keyboard notifications to scroll your scrollview into place – Jamie Jul 03 '12 at 22:43
1 Answers
2
Try This
#define kKeyboardAnimationDuration 0.3
@interface YourViewController:UIViewController
{
BOOL keyboardIsShown;
}
And then in your implementation
- (void)viewDidLoad
{
// register for keyboard notifications
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(keyboardWillShow:)
name:UIKeyboardWillShowNotification
object:self.view.window];
// register for keyboard notifications
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(keyboardWillHide:)
name:UIKeyboardWillHideNotification
object:self.view.window];
keyboardIsShown = NO;
}
- (void)viewDidUnload
{
[super viewDidUnload];
// Release any retained subviews of the main view.
// e.g. self.myOutlet = nil;
// unregister for keyboard notifications while not visible.
[[NSNotificationCenter defaultCenter] removeObserver:self
name:UIKeyboardWillShowNotification
object:nil];
// unregister for keyboard notifications while not visible.
[[NSNotificationCenter defaultCenter] removeObserver:self
name:UIKeyboardWillHideNotification
object:nil];
}
#pragma mark - Keyboard Events
- (void)keyboardWillShow:(NSNotification *)n
{
if (keyboardIsShown)
{
return;
}
NSDictionary* userInfo = [n userInfo];
CGSize keyboardSize = [[userInfo objectForKey:UIKeyboardFrameBeginUserInfoKey] CGRectValue].size;
CGRect viewFrame = self.view.frame;
viewFrame.origin.y-= (keyboardSize.height);
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationBeginsFromCurrentState:YES];
[UIView setAnimationDuration:kKeyboardAnimationDuration];
[self.view setFrame:viewFrame];
[UIView commitAnimations];
keyboardIsShown = YES;
}
- (void)keyboardWillHide:(NSNotification *)n
{
NSDictionary* userInfo = [n userInfo];
CGSize keyboardSize = [[userInfo objectForKey:UIKeyboardFrameBeginUserInfoKey] CGRectValue].size;
CGRect viewFrame = self.view.frame;
viewFrame.origin.y+= (keyboardSize.height);
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationBeginsFromCurrentState:YES];
[UIView setAnimationDuration:kKeyboardAnimationDuration];
[self.view setFrame:viewFrame];
[UIView commitAnimations];
keyboardIsShown = NO;
}

Saikat
- 2,613
- 1
- 22
- 14
-
Thanks! The only things left, is that I'll need to get keyboard will hide working, but that should be too hard. – sebi Jul 04 '12 at 02:27
-
Do you mean keyboardWillHide is not getting called? Any error messages? – Saikat Jul 04 '12 at 03:46
-
I got everything working, but one more question if you don't mind, how can I get only the text box to move up with the keyboard and not the whole view, I almost got it when I changed [self.view setFrame:viewFrame]; to [pretextField setFrame:viewFrame]; with pretextField being a textfield, thanks! – sebi Jul 04 '12 at 06:30
-
Actually I got it! I'm not sure why I have to subtract 392 from the keyboard height, but maybe I'll figure that out, I had to do this: CGRect viewFrame = CGRectMake(0, 0, 320, 24); viewFrame.origin.y-= (keyboardSize.height - 392); – sebi Jul 04 '12 at 06:40