It should be as simple as :
- (void)setScrollView:(UIScrollView *)scrollView bottomOffsetToView:(UIView *)view
{
scrollView.contentOffset = CGPointMake(0, view.frame.origin.y + view.frame.size.height - 216);
}
then in your code set yourself as UITextFieldDelegate and implement :
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField
{
self.currentTextField = textField;
}
Update :
You ask two different things here.. For the keyboard register for keyboard notifications (in viewWillAppear
for instance) :
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWasShown:) name:UIKeyboardDidShowNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWasHidden:) name:UIKeyboardWillHideNotification object:nil];
and then change the scrollview frame according to the keyboard size :
- (void)keyboardWasShown:(NSNotification*)aNotification
{
NSDictionary* info = [aNotification userInfo];
CGSize kbSize = [[info objectForKey:UIKeyboardFrameBeginUserInfoKey] CGRectValue].size;
self.scrollView.frame = (CGRect){self.scrollView.frame.origin,(CGSize){self.scrollView.frame.size.width,self.scrollView.frame.size.height-kbSize.height);
if (self.currentTextField != nil)
{
[self setScrollView:self.scrollView bottomOffsetToView:self.currentTextField];
}
}
Same behavior for keyboardWasHidden (resetting initial frame) and don't forget to remove Observer in viewDidDisappear.