Reputation: 1149
I have few uitextfields in my ui where keyboard will overlaps few of the uitextfields which are at bottom of my screen, to handle this i have implemented UIKeyboard notifications like
- (void)keyboardWasShown:(NSNotification*)aNotification
- (void)keyboardWillBeHidden:(NSNotification*)aNotification
and my uitextfields keyboard returnKeyType is UIReturnKeyNext so when ever user taps on Next button i'm making my next textfield as becomeFirstResponder but when the textfield which is being hidden from keyboard becomes first responder its frame is not changing and updating to visible area boundary out of uikeyboard since the notification triggers only for the first time when i tap on a textfield. I need to trigger this keyboardWasShown method on every becomeFirstResponder event. Any help is appreciated in advance.
Upvotes: 1
Views: 500
Reputation: 11696
This is how I implemented my code:
1) My view is a UIScrollView.
2) I have multiple UITextField in my view and use tag IDs to tell them apart. When the user taps on a UITextField, this delegate is called to move the UIScrollView:
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField {
switch (textField.tag)
{
case 0:
[myScrollView setContentOffset:CGPointMake(0, 18) animated:YES];
break;
case 1:
[myScrollView setContentOffset:CGPointMake(0, 56) animated:YES];
break;
case 2:
[myScrollView setContentOffset:CGPointMake(0, 94) animated:YES];
break;
case 3:
[myScrollView setContentOffset:CGPointMake(0, 132) animated:YES];
break;
case 4:
[myScrollView setContentOffset:CGPointMake(0, 170) animated:YES];
break;
case 5:
[myScrollView setContentOffset:CGPointMake(0, 208) animated:YES];
break;
case 6:
[myScrollView setContentOffset:CGPointMake(0, 246) animated:YES];
break;
case 7:
[myScrollView setContentOffset:CGPointMake(0, 284) animated:YES];
break;
case 8:
[myScrollView setContentOffset:CGPointMake(0, 322) animated:YES];
break;
default:
break;
}
return YES;
}
3) I also have UITextView and use this delegate to achieve the same effect:
- (BOOL)textViewShouldBeginEditing:(UITextView *)textView {
if(textView.tag == 0)
{
[myScrollView setContentOffset:CGPointMake(0, 360) animated:YES];
// do some stuff...
}
if(textView.tag == 1)
{
[myScrollView setContentOffset:CGPointMake(0, 504) animated:YES];
// do some stuff...
}
return YES;
}
Note: The CGPointMake(0, 123)
values depend on your own text field coordinates.
Upvotes: 0
Reputation: 10175
First make sure that your code in those two methods is correct and take in consideration that when you change the first responder from a UITextField to another there will be two notifications sendet, one for keyboard hidden and one for keyboard shown.
I don't know how you handle this notification and if you have access to the text fields, but I managed to implement the scrolling
behaviour for some text fileds using just the delegate methods of UITextFields
-(BOOL)textFieldShouldReturn:(UITextField *)textField
(move content down because the keyboard is hidden)
-(void)textFieldDidBeginEditing:(UITextField *)textField
(move content up because the keyboard will appear)
Using this methods you can handle the content movement even if your text field has set as inputView other views like a UIPickerView.
Upvotes: 1
Reputation: 4953
i would suggest use a drop-In replacement Library TPKeyboardAvoiding, which will handle all the moving of the view's. if, the textfield will be hidden due to onscreen keyboard. Its very easy to use, And works Well.
Upvotes: 0