SlumTheSlug
SlumTheSlug

Reputation: 157

iOS Static cell, automatic scroll, and additional toolbar keyboard

I'm currently coding an app that is mostly composed of forms. To do the job I'm using static cells with an UITableViewController, that can contains UITextField and UITextView. If you're using the default configuration of the language everything works reasonably good. But for the UITextView, the weak is you can't hide the keyboard, because taping on the return key will jump to next line. So I put a toolbar to enable the dismiss keyboard feature (with NSNotification on keyboard). But when it scrolls to the cell that contains the textfield, the field is hidden by the toolbar, the scroll doesn't add the height of the toolbar. Screenshots : Before clicking on the field https://dl.dropbox.com/u/9858108/tableViewIssue1.jpg After clicking on the field https://dl.dropbox.com/u/9858108/tableViewIssue2.jpg

Anyone has a magic code snippet to do the trick ?

Upvotes: 0

Views: 623

Answers (1)

SlumTheSlug
SlumTheSlug

Reputation: 157

A., thank you for the tip, I modified the code and now it works. Here is the working code :

/**
 * Cette méthode affiche la toolbar pour terminer l'adition quand le clavier est affiché
 *
 * @param NSNotification notification Notification de l'émetteur
 */
- (void)keyboardWillShow:(NSNotification *)notification {
    [UIView beginAnimations:nil context:NULL];
    [UIView setAnimationDuration:0.3];

    CGRect frame = self.toolbarAction.frame;
    frame.origin.y = self.parentViewController.view.frame.size.height - 260.0;
    self.toolbarAction.frame = frame;

    // Cette portion de code permet de remonter le scroll (à cause de la toolbar)
    if (![[AppKit sharedInstance] isIPad]) {
        CGRect tableFrame = self.tableView.frame;
        tableFrame.origin.y = tableFrame.origin.y - 50;
        self.tableView.frame = tableFrame;
    }

    [UIView commitAnimations];

    // Action pour les keyboards
    self.toolbarDoneButton.tag = 1;
}

/**
 * Cette méthode cache la toolbar lorsque le clavier n'est plus affiché
 *
 * @param NSNotification notification Notification de l'émetteur
 */
- (void)keyboardWillHide:(NSNotification *)notification {
    [UIView beginAnimations:nil context:NULL];
    [UIView setAnimationDuration:0.3];

    CGRect frame = self.toolbarAction.frame; 
    frame.origin.y = self.parentViewController.view.frame.size.height;
    self.toolbarAction.frame = frame;

    // Cette portion de code permet de rebaisser le scroll (à cause de la toolbar)
    if (![[AppKit sharedInstance] isIPad]) {
        CGRect tableFrame = self.tableView.frame;
        tableFrame.origin.y = tableFrame.origin.y + 50;
        self.tableView.frame = tableFrame;
    }

    [UIView commitAnimations];
}

The interesting part that fix the issue is :

CGRect tableFrame = self.tableView.frame;
tableFrame.origin.y = tableFrame.origin.y - 50;
self.tableView.frame = tableFrame;

Upvotes: 1

Related Questions