Programmatically change UITableView scroll tabs to show iPhone keyboard size

I know that you can use the "Insert" property in Interface Builder to make the scroll view nested from the main window so that it does not go beyond existing on-screen controls such as the tab bar, but how can you do this programmatically. when is the keyboard added to the screen? Currently, my scroll view has cells under the keyboard that are not accessible because the view still registers the bottom of the scroll view as the bottom of the phone and not the top of the keyboard.

+2


source to share


4 answers


#pragma mark Keyboard Handling



- (void) viewDidAppear:(BOOL) ani {
    onscreen = YES;

    NSNotificationCenter* center = [NSNotificationCenter defaultCenter];
    [center addObserver:self selector:@selector(keyboardWillAppear:) name:UIKeyboardWillShowNotification object:nil];
    [center addObserver:self selector:@selector(keyboardDidAppear:) name:UIKeyboardDidShowNotification object:nil];
    [center addObserver:self selector:@selector(keyboardWillDisappear:) name:UIKeyboardWillHideNotification object:nil];
}

- (void) viewDidDisappear:(BOOL) ani {
    onscreen = NO;

    NSNotificationCenter* center = [NSNotificationCenter defaultCenter];
    [center removeObserver:self name:UIKeyboardWillShowNotification object:nil];
    [center removeObserver:self name:UIKeyboardDidShowNotification object:nil];
    [center removeObserver:self name:UIKeyboardWillHideNotification object:nil];
}

- (void) keyboardWillAppear:(NSNotification*) n {
    NSLog(@"Keyboard is about to appear");
}

- (void) keyboardDidAppear:(NSNotification*) n {

    CGRect bounds = [[[n userInfo] objectForKey:UIKeyboardBoundsUserInfoKey] CGRectValue];
    bounds = [self.view convertRect:bounds fromView:nil];

    CGRect tableFrame = searchResultsTable.frame;
    tableFrame.size.height -= bounds.size.height; // subtract the keyboard height
    if (self.tabBarController != nil) {
        tableFrame.size.height += 48; // add the tab bar height
    }

    [UIView beginAnimations:nil context:NULL];

    [UIView setAnimationDelegate:self];
    [UIView setAnimationDidStopSelector:@selector(shrinkDidEnd:finished:contextInfo:)];
    searchResultsTable.frame = tableFrame;
    [UIView commitAnimations];

    //[self hideEditorView:currentEditorView];
    //[currentEditorView removeFromSuperview];
}

- (void) shrinkDidEnd:(NSString*) ident finished:(BOOL) finished contextInfo:(void*) nothing {
    NSIndexPath* sel = [searchResultsTable indexPathForSelectedRow];

    if (![[searchResultsTable indexPathsForVisibleRows] containsObject:sel])
    {
        [searchResultsTable scrollToNearestSelectedRowAtScrollPosition:UITableViewScrollPositionMiddle animated:YES];
    }
}

- (void) keyboardWillDisappear:(NSNotification*) n {
    CGRect bounds = [[[n userInfo] objectForKey:UIKeyboardBoundsUserInfoKey] CGRectValue];
    bounds = [self.view convertRect:bounds fromView:nil];

    CGRect tableFrame = searchResultsTable.frame;
    tableFrame.size.height += bounds.size.height; // add the keyboard height

    if (self.tabBarController != nil) {
        tableFrame.size.height -= 48; // subtract the tab bar height
    }

    [UIView beginAnimations:nil context:NULL];

    [UIView setAnimationDelegate:self];
    [UIView setAnimationDidStopSelector:@selector(shrinkDidEnd:finished:contextInfo:)];
    searchResultsTable.frame = tableFrame;    
    [UIView commitAnimations];

    [searchResultsTable scrollToNearestSelectedRowAtScrollPosition:UITableViewScrollPositionMiddle animated:YES];
}

      



+7


source


Two options. (when you say scrolling I think you mean tabular view, as the title suggests so)

  • Use UITableViewController

    if keyboard is added to table view, i believe it will do the resizing for you

  • In the textbox or textbox in the method, - (void)textFieldDidBeginEditing:(UITextField *)textField

    resize the tableview and end finished editing to set it back (you can also do this if its the scroll view and not the tableView)

    • (void) textFieldDidBeginEditing: (UITextField *) textField {CGRect frame = tableView.frame; // or scroll view frame.size.height = frame.size.height- keyboardHeight; tableView.frame = frame}

    • (void) textFieldDidEndEditing: (UITextField *) textField {CGRect frame = tableView.frame; // or scroll view frame.size.height = frame.size.height + keyboardHeight; tableView.frame = frame}



Hope it helps

+1


source


Another easy way with Interface Builder or code

Set the [ Table Footer View Height ] property to 200 (play with this for the layout). This actually makes the table larger than the bounding frame it is contained within, allowing the table to scroll higher than usual.

Great for allowing the user to scroll up and down the table as needed.

0


source


I have created a small project that solves this problem with a keyboard, in my case I only need to enlarge the table view when the keyboard appears.

Hope this helps!

http://git.io/BrH9eQ

0


source







All Articles