11

I have a UITableViewController with a grouped static UITableView. I am defining the cells for my static table view on the storyboard. One of the cells has a textfield in it. When this textfield is called, the keyboard pops up, however, the tableview is not automatically resizing like it normally would on a table view controller. So now the keyboard is partially covering the textfield and I can't scroll up.

My understanding is that when you are using a UITableViewController and a tableview, the viewable area should automatically shrink when the keyboard is called. It does work as intended in other parts of my app, just not with this static table view. Does it not work with static tables? Is there something else I am missing? Is there an easy way to solve this?

Thanks

Dustin
  • 6,783
  • 4
  • 36
  • 53
Keith
  • 1,352
  • 3
  • 21
  • 48
  • You can either move the view up or manually resize it. – Dustin Jul 03 '12 at 15:23
  • 1
    The question is why is it not automatically doing it when it normally does? – Keith Jul 03 '12 at 15:34
  • Probably because you are using a static table view (like you guessed). Static table views are specifically designed to not change - which is why they are called "static" – Dustin Jul 03 '12 at 15:39
  • Is there a current, good easy answer on how to do this? I have looked and most answers don't seem to work with table views (mainly scroll views). – Keith Jul 03 '12 at 17:21
  • Just so you know, `UITableView` inherits from `UIScrollView` so you can do exactly the same things to it as you would do to a `UIScrollView`. – Dustin Jul 03 '12 at 17:25
  • I created a test project with static cells and it works fine so it's definitely something with my project. I'm just not sure what. – Keith Jul 05 '12 at 17:23
  • try this https://stackoverflow.com/a/61875191/6314955 – Malith Kuruwita May 18 '20 at 17:20

3 Answers3

56

Answer

It has nothing to do with static cells. They should work.

If your controller is already a UITableViewController, check if you used the method viewWillAppear. If you did, you have to call [super viewWillAppear:YES] to get the 'automatic behavior' to work.

-(void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:YES]; // This line is needed for the 'auto slide up'
   // Do other stuff
}

This problem turns up easily because the boilerplate code for the controllers don't come with the viewWillAppear method call and if you define it in your controller, you override it.

Extra Information

Look at this link.

Apple Table View Programming Guide

Note: UITableViewController has new capabilities in iOS 3.0. A table-view controller supports inline editing of table-view rows; if, for example, rows have embedded text fields in editing mode, it scrolls the row being edited above the virtual keyboard that is displayed.... blah....

The important bit

The UITableViewController class implements the foregoing behavior by overriding loadView, viewWillAppear:, and other methods inherited from UIViewController. In your subclass of UITableViewController, you may also override these methods to acquire specialized behavior. If you do override these methods, be sure to invoke the superclass implementation of the method, usually as the first method call, to get the default behavior.

Damon Aw
  • 4,722
  • 4
  • 28
  • 37
  • I found out not long after I posted this here that it had nothing to do with the static table. I created a test static table and it worked fine so I knew it was something with my code. You were 100% right. It was the [super viewWillAppear:YES];. As soon as I added that to my viewWillAppear it worked. I guess some how it got deleted. I was dreading on fixing this week and it turned out to be really easy. Thanks – Keith Jul 09 '12 at 15:10
  • 1
    Cheers man. Your question pointed me to the docs and I learnt another gotcha =) – Damon Aw Jul 09 '12 at 16:47
  • 1
    Great answer, scandalous you haven't had more up votes. Here is one anyway. – Gordon Dove Aug 14 '12 at 08:44
  • @GordonDove Thanks. I made it a little more concise for future readers. – Damon Aw Aug 14 '12 at 11:39
  • YOU ARE A SEER!!?!?! A damn copy&paste had me put super.viewWillDisappear in viewWillAppear. Thanks to your advice, the keyboard not behaves like a good citizen. UpVote. – Jean Le Moignan Oct 24 '14 at 13:30
  • Thanks, can't believe that this was the deal breaker. – Ace Green Oct 06 '15 at 11:49
2

For Swift

override func viewWillAppear(animated: Bool) {

   super.viewWillAppear(animated)

}
anka
  • 3,817
  • 1
  • 30
  • 36
Paul S.
  • 1,342
  • 4
  • 22
  • 43
0

Pushes the view up if one of the table forms is selected for editing (requires keyboard notification implementation)

- (void) keyboardDidShow:(NSNotification *)aNotification
{
    [UIView beginAnimations:nil context:NULL];
    [UIView setAnimationDuration:0.25];
    self.view.center = CGPointMake(self.view.center.x, self.view.center.y-moveAmount);
    [UIView commitAnimations];
    isRaised = [NSNumber numberWithBool:YES];
}

Resizes the table (divides height by 2). Swap this into the keyboard did show method. Also, you can use the keyboard did hide method to undo this stuff.

CGRect temp = CGRectMake(mineTable.frame.origin.x, mineTable.frame.origin.y, mineTable.frame.size.width, mineTable.frame.size.height/2);
mineTable.frame = temp;
Dustin
  • 6,783
  • 4
  • 36
  • 53
  • 1
    Is this the full answer? Seems like some things are missing. – Keith Jul 03 '12 at 17:21
  • Well I didn't want to show you a bunch of stuff that you already know how to code. I did tell you what to do (implement keyboard notifications). It's just some example code to point you in the right direction. – Dustin Jul 03 '12 at 17:23