dermdaly
dermdaly

Reputation: 529

Another take on the "keyboard obscures UITextField" problem

I know that this is a common problem and the UITableViewController fixed this is iPhone SDK 3.0, but the UITableViewController is not working as I expect, probably due to how I am using it. Here's my problem:

I'm working on a form, which is in a grouped table, which contains some text fields. Those on the lower part of the form get obscured by the keyboard. I know this is an age-old problem, and there are some examples of code to auto scroll, but I've a little bit more to add, and was wondering if anyone has found this, and fixed it.

Firstly: Since 3.0, if your table is controlled by a UITableViewController, you get this scrolling automagically. I've tried it and indeed it does work. However, I want to use a custom background image, which UITableViewController doesn't play ball with. Here's what I've done:

  1. Create the XIB with a plain view, onto which there's an image.
  2. Also inside the XIB, I've a UIViewController (in fact a my-own subclass as I write the data source methods, etc.), which has the table.
  3. In my main view viewDidLoadMethod I've tried this: tableViewController.view.backgroundColor = [UIColor clearColor]; [self.view addSubview:tableViewController.view];

And indeed, the table shows up as I expect it to look. However, when I tap on a text field in the table, I'm not getting the "magic" scrolling that the tableViewController is meant to give me for free.

As it stands, it is the exact same behaviour as I didn't use a TableViewController at all, and dropped the table directly onto the imageView. (Which, frankly, makes for 1 less class, and easier to read code).

The only reason I introduced a TableViewController was to get this auto-scrolling.

Is there something I am missing?

Upvotes: 4

Views: 1428

Answers (3)

Reuven
Reuven

Reputation: 2132

(Repeating my answer from another SO question):

I personally highly recommend using TPKeyboardAvoiding by Michael Tyson.

It's very easy to use... (quoting from the Read Me):

For use with UITableViewController classes, drop TPKeyboardAvoidingTableView.m and TPKeyboardAvoidingTableView.h into your project, and make your UITableView a TPKeyboardAvoidingTableView in the xib. If you're not using a xib with your controller, I know of no easy way to make its UITableView a custom class: The path of least resistance is to create a xib for it.

For non-UITableViewControllers, drop the TPKeyboardAvoidingScrollView.m and TPKeyboardAvoidingScrollView.h source files into your project, pop a UIScrollView into your view controller's xib, set the scroll view's class to TPKeyboardAvoidingScrollView, and put all your controls within that scroll view. You can also create it programmatically, without using a xib - just use the TPKeyboardAvoidingScrollView as your top-level view.

Upvotes: 0

iwat
iwat

Reputation: 3831

Resize it; if you want it smooth then use animation block.

[UIView beginAnimations:@"tableAnim" context:nil];
// 214 = keyboard size, adjust it if you have navigation bar or status bar
tableView.frame = CGRectMake(0, 0, 320, 480 - 214);
[UIView commitAnimations];

Upvotes: 1

JOM
JOM

Reputation: 8207

If you select the last item in table, it really cannot scroll above keyboard, because there's not enough table to scroll. It does scroll as far as it can, but that might not be enough to bring the selected item visible.

What I did was resize the table to be fully visible just above the keyboard. It's not as smooth as it should be, but good enough == each and every item can be visible.

Upvotes: 0

Related Questions