Reputation: 428
i have a problem with keyboard appearing in UiscrollView.
i added a UIScrollview as
scrlView=[[UIScrollView alloc] initWithFrame:CGRectMake(10, 140, 1000, 600)];
scrlView.scrollEnabled=YES;
scrlView.showsVerticalScrollIndicator=YES;
scrlView.bounces=NO;
to this scrollView i have added 10 rows of UITextFields each row has 5 textFields each textfield height is 50px. When ever trying to edit textfield it is overlapping by keyBoard.For that i tried this code
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(keyboardWasShown:)
name:UIKeyboardDidShowNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(keyboardWillBeHidden:)
name:UIKeyboardWillHideNotification object:nil];
- (void)keyboardWasShown:(NSNotification*)aNotification
{
NSDictionary* info = [aNotification userInfo];
CGSize kbSize = [[info objectForKey:UIKeyboardFrameBeginUserInfoKey] CGRectValue].size;
CGRect bkgndRect = selectetTxtfld.superview.frame;
bkgndRect.size.height += kbSize.height;
[selectetTxtfld.superview setFrame:bkgndRect];
[scrlView setContentOffset:CGPointMake(0.0, selectetTxtfld.frame.origin.y) animated:YES];
}
}
// Called when the UIKeyboardWillHideNotification is sent
- (void)keyboardWillBeHidden:(NSNotification*)aNotification
{
UIEdgeInsets contentInsets = UIEdgeInsetsZero;
[UIView animateWithDuration:0.4 animations:^{
scrlView.contentInset = contentInsets;
}];
scrlView.scrollIndicatorInsets = contentInsets;
}
But the textField not appearing on the keyboard.it appearing at the scrollview ypoint position
help me on this problem.I saw many answers in StackOverFlow.But not cleared my problem
Upvotes: 0
Views: 290
Reputation: 1191
- (void)keyboardWillBeHidden:(NSNotification*)aNotification {
NSDictionary* info = [aNotification userInfo];
CGSize kbSize = [[info objectForKey:UIKeyboardFrameBeginUserInfoKey] CGRectValue].size;
CGFloat offSetAfterKeyboardIsDisplayed = scrlview.contentOffset.y + kbSize.height;
[UIView animateWithDuration:0.3 animations:^{
//adding content inset at the bottom of the scrollview
scrlView.contentInset = UIEdgeInsetMake(0,0,kbSize.height,0);
[scrlview setContentOffset:offSetAfterKeyboardIsDisplayed]
}];
}
- (void)keyboardWillBeHidden:(NSNotification*)aNotification
{
NSDictionary* info = [aNotification userInfo];
CGSize kbSize = [[info objectForKey:UIKeyboardFrameBeginUserInfoKey] CGRectValue].size;
CGFloat offSetAfterKeyboardResigns = scrlview.contentOffset.y - kbSize.height;
[UIView animateWithDuration:0.3 animations:^{
scrlView.contentInset = UIEdgeInsetsZero;
[scrlview setContentOffset:offSetAfterKeyboardResigns]
}];
}
Upvotes: 1
Reputation: 1191
in keyboardWasShown: 1.add content inset at bottom of the scrollview with value equal to the height of the keyboard. 2. setContentOffset = current offset+ height of the keyboard. Note: 1&3 should be done in an animation block with duration equalto 0.30
in keyboardWillBeHidden: 1.set contentInset = UIEdgeInsetsZero 2. setContentOffset = current offset - height of the keyboard. Note: 1&3 should be done in an animation block with duration equalto 0.30
This should solve ur problem :)
Upvotes: 1