Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Scroll the table view to the pop over-presenting text field when keyboard fires up by a text field inside the pop over

I have a UITableViewController with many cells, each cell contains four text fields vertical on each other, a pop over is presented by tapping on any text field, however, this pop over contains a text field when tapped the keyboard is fired and most likely the pop over will be shifted up to prevent the keyboard from hiding its text field (this is the default behavior of the pop over), but in the background (the dimmed view), the tableViewController loses its correct scrolling behavior to keep the pop over presenting-textField on the track while the keyboard is visible ..

a sample project can be downloaded here.

how can I offset the table view to keep the pop over presenting-textField on screen while keyboard is visible in this case ?

I tried the well-known TPKeyboardAvoiding library but it didn't solve the issue.

p.s. tableViewController works well for the first 3 or 4 keyboard firings, but loses precise scrolling on later attempts.

Screenshot (the green text field is the text field which presented the pop over, but tableViewController scrolls to the incorrect text field indicated in red): enter image description here

any help would be highly appreciated.

EDIT: this question is not a duplicate for: Making a UITableView scroll when text field is selected because the text field that I need the table view to scroll to is the one that fires a pop over not a keyboard, and scrollToRowAtIndexPath does not work precisely in this case because each cell contains 4 text fields.

like image 664
JAHelia Avatar asked Dec 29 '14 14:12

JAHelia


2 Answers

use (CGRect)convertRect:(CGRect)rect toView:(UIView *)view to get cell position on tableviews superview and accordingly handle the tableview offset

like image 182
vje1998 Avatar answered Sep 28 '22 10:09

vje1998


Here is my solution : Change into TableViewController.m

1. Register for keyboard Notifications (UIKeyboardWillShowNotification, UIKeyboardWillHideNotification)

2. Create local variables:

CGSize _currentPopoverContentSize; //if you want to have custom size for popover

UIView *_currentPopoverSender; //to remember from wich view you will present popover

BOOL _keyboardIsShown; //enable in keyboardWillShow, and disable in keyboardWillHide

3. In my presentPopover method:

- (void)presentPopoverControllerWithSize:(CGSize)size fromView:(UIView *)sender{

MyController *controller = [[[MyController alloc] init] autorelease];

if (self.popover)
{
    [_popover release];
    _popover = nil;
}

_popover = [[UIPopoverController alloc] initWithContentViewController:controller];
_popover.popoverContentSize = size;
_popover.delegate = self;

//checking if keyboard is shown - if NO, than present popover, if YES - just `resignFirstResponder` for your _`activeTextField`(you can set it in -textFieldDidBeginEditing: and nullify in -textFieldDidEndEditing:)
if (!_keyboardIsShown)
{
    [_popover presentPopoverFromRect:[sender bounds]
                              inView:sender
            permittedArrowDirections:UIPopoverArrowDirectionUp
                            animated:YES];
    _popOver.popoverContentSize = CGSizeMake(320, 700);

}
else
{
    [_activeTextField resignFirstResponder];
}

_currentPopoverContentSize = size;
_currentPopoverSender = sender;
}

4. Than:

- (void)keyboardWillBeHidden:(NSNotification*)aNotification{

[UIView animateWithDuration:0.3
                 animations:^{
                     //do some stuff
                  _popOver.popoverContentSize = CGSizeMake(320, 700);

                 } completion:^(BOOL finished) {

                     if (_popover && _currentPopoverSender)
                     {
                         [_popover presentPopoverFromRect:[_currentPopoverSender bounds]
                                                   inView:_currentPopoverSender
                                 permittedArrowDirections:UIPopoverArrowDirectionUp
                                                 animated:YES];
                     }

                 }];

_keyboardIsShown = NO;
}

5.:

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

    [textField resignFirstResponder];
    [self presentPopoverControllerWithSize:textField.frame.size fromView:_activeText];

//     self.vc = [[self storyboard] instantiateViewControllerWithIdentifier:@"ProductSourcePopOver"];
//
//    if(_popOver == nil){   //make sure popover isn't displayed more than once in the view
//        _popOver = [[UIPopoverController alloc]initWithContentViewController:self.vc];
//    }
//    _popOver.popoverContentSize = CGSizeMake(320, 700);
//
//    [_popOver presentPopoverFromRect:_activeText.frame inView:_activeText permittedArrowDirections:UIPopoverArrowDirectionLeft animated:YES];
//    _popOver.delegate = self;

}

This might helps you :)

like image 24
Yuyutsu Avatar answered Sep 28 '22 12:09

Yuyutsu