Reputation: 14418
I was following the code here
#pragma mark UITextFieldDelegate
- (BOOL)textFieldShouldReturn:(UITextField *)textField {
[textField resignFirstResponder];
return YES;
}
-(void)textFieldDidBeginEditing:(UITextField *)sender
{
if ([sender isEqual:password])
{
//move the main view, so that the keyboard does not hide it.
if (self.view.frame.origin.y >= 0)
{
[self setViewMovedUp:YES];
}
}
}
//method to move the view up/down whenever the keyboard is shown/dismissed
-(void)setViewMovedUp:(BOOL)movedUp
{
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:0.5]; // if you want to slide up the view
CGRect rect = self.view.frame;
if (movedUp)
{
// 1. move the view's origin up so that the text field that will be hidden come above the keyboard
// 2. increase the size of the view so that the area behind the keyboard is covered up.
rect.origin.y -= kOFFSET_FOR_KEYBOARD;
rect.size.height += kOFFSET_FOR_KEYBOARD;
}
else
{
// revert back to the normal state.
rect.origin.y += kOFFSET_FOR_KEYBOARD;
rect.size.height -= kOFFSET_FOR_KEYBOARD;
}
self.view.frame = rect;
[UIView commitAnimations];
}
- (void)keyboardWillShow:(NSNotification *)notif
{
//keyboard will be shown now. depending for which textfield is active, move up or move down the view appropriately
if ([password isFirstResponder] && self.view.frame.origin.y >= 0)
{
[self setViewMovedUp:YES];
}
else if (![password isFirstResponder] && self.view.frame.origin.y < 0)
{
[self setViewMovedUp:NO];
}
}
- (void)viewWillAppear:(BOOL)animated
{
// register for keyboard notifications
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillShow:)
name:UIKeyboardWillShowNotification object:self.view.window];
}
- (void)viewWillDisappear:(BOOL)animated
{
// unregister for keyboard notifications while not visible.
[[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillShowNotification object:nil];
}
It actually moves the view up when I tap on the password UITextField, however after I finish it did not scroll down again. What am I missing here?
Upvotes: 0
Views: 2387
Reputation: 25
Try
-(void)textFieldDidEndEditing:(UITextField *)sender
{
// Your code to scroll down your view.
}
It written and not tested yet.
Upvotes: 1
Reputation: 4038
I'm not sure how to fix your code issue. But, there is a easy way. You may wanna try the highly recommended "TPKeyboardAvoidingScrollView": https://github.com/michaeltyson/TPKeyboardAvoiding
Upvotes: 0
Reputation: 883
You implemented keyboardWillShow. You also have to implement keyboardwillHide.
Check this post:
Making a UITableView scroll when text field is selected
This worked great for me.
Upvotes: 1