I have a scrollview and a few UITextFields in it. When I touch inside the UITextField, they keyboard comes up but it obscures some of the UITextFields. I want the text fields to scroll up when the keyboard appears and scroll down when it disappears. I have done this but one thing is not happening. Only when I start typing, the text fields scroll up and the keyboard hides them until then. Rather they have to scroll up as soon as the keyboard appears. I am pasting the code below. Other than this issue, keyboard appears and disappears properly and the text fields scrolls up when I start typing and comes down when I am done.
Here is the code:
Code:
-(void) viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
[[NSNotificationCenter defaultCenter]
addObserver:self
selector:@selector
(keyboardWillShow:)
name:UIKeyboardWillShowNotification
object:nil];
[[NSNotificationCenter defaultCenter]
addObserver:self
selector:@selector
(keyboardDidHide:)
name:UIKeyboardDidHideNotification
object:nil];
scrollView.contentSize = CGSizeMake(320.0,450.0);
displayKeyboard = NO;
}
-(void) keyboardWillShow: (NSNotification *)notif {
if(displayKeyboard)
return;
NSDictionary* info = [notif userInfo];
NSValue* aValue = [info objectForKey:UIKeyboardFrameEndUserInfoKey];
CGSize keyboardSize = [aValue CGRectValue].size;
offset = scrollView.contentOffset;
CGRect viewFrame = scrollView.frame;
viewFrame.size.height -= keyboardSize.height;
scrollView.frame = viewFrame;
CGRect textFieldRect = [field frame];
textFieldRect.origin.y += 5;
[scrollView scrollRectToVisible: textFieldRect animated:YES];
displayKeyboard = YES;
}
Appreciate your help!!
Thanks,