Monday, 30 March 2015

Scroll UITextField above Keyboard in a UITableView OR UIScrollView

-(BOOL)textFieldShouldBeginEditing:(UITextField *)textField {
    CGPoint pointInTable = [textField.superview convertPoint:textField.frame.origin toView:TableName];
    CGPoint contentOffset = TableName.contentOffset;
 
    contentOffset.y = (pointInTable.y - textField.inputAccessoryView.frame.size.height);
 
    NSLog(@"contentOffset is: %@", NSStringFromCGPoint(contentOffset));  
    [TableName setContentOffset:contentOffset animated:YES];  
    return YES;
}


-(BOOL)textFieldShouldEndEditing:(UITextField *)textField
{
    [textField resignFirstResponder];  
    if ([textField.superview.superview isKindOfClass:[UITableViewCell class]])
    {
        CGPoint buttonPosition = [textField convertPoint:CGPointZero
                                                  toView: TableName];
        NSIndexPath *indexPath = [TableName indexPathForRowAtPoint:buttonPosition];
     
        [TableName scrollToRowAtIndexPath:indexPath atScrollPosition:UITableViewScrollPositionMiddle animated:TRUE];
    }
 
    return YES;
}


(OR)

- (void)textFieldDidBeginEditing:(UITextField *)textField {

    CGPoint pointInTable = [textField.superview convertPoint:textField.frame.origin toView:scheduleTableView];
    CGPoint contentOffset = scheduleTableView.contentOffset;
    
    contentOffset.y = (pointInTable.y - textField.inputAccessoryView.frame.size.height);
    [scheduleTableView setContentOffset:contentOffset animated:YES];
}

- (void)textFieldDidEndEditing:(UITextField *)textField {
    [textField resignFirstResponder];
    if ([textField.superview.superview isKindOfClass:[UITableViewCell class]])
    {
        CGPoint buttonPosition = [textField convertPoint:CGPointZero
                                                  toView: scheduleTableView];
        NSIndexPath *indexPath = [scheduleTableView indexPathForRowAtPoint:buttonPosition];
        
        [scheduleTableView scrollToRowAtIndexPath:indexPath atScrollPosition:UITableViewScrollPositionMiddle animated:YES];
    }
    

}

No comments:

Post a Comment