After a lot of trial and error, I'm giving up and asking the question. I've seen a lot of people with similar problems but can't get all the answers to work right.
I have a UITableView
which is composed of custom cells. The cells are made of 5 text fields next to each other (sort of like a grid).
When I try to scroll and edit the cells at the bottom of the UITableView
, I can't manage to get my cells properly positioned above the keyboard.
I have seen many answers talking about changing view sizes,etc... but none of them has worked nicely so far.
Could anybody clarify the "right" way to do this with a concrete code example?
Here is how I made this work, which is a mixture of Sam Ho and Marcel W's answers, and some of my own bug fixes made to my crappy code. I was using a UITableViewController. The table now resizes correctly when the keyboard is shown.
1) In
viewDidLoad
I added:self.tableView.autoresizingMask = UIViewAutoresizingFlexibleHeight;
2) I had forgotten to call the
super
equivalents inviewWillAppear
andawakeFromNib
. I added these back in.If you use a uitableview to place your textfields (from Jeff Lamarche), you can just scroll the tableview using the delegate method like so.
(Note: my text fields are stored in an array with the same index as there row in the tableview)
This soluton works for me, PLEASE note the line
You can change the 160 value to match it work with you
If your UITableView is managed by a subclass of UITableViewController and not UITableView, and the text field delegate is the UITableViewController, it should manage all the scrolling automatically -- all these other comments are very difficult to implement in practice.
For a good example see the apple example code project: TaggedLocations.
You can see that it scrolls automatically, but there doesn't seem to be any code that does this. This project also has custom table view cells, so if you build your application with it as a guide, you should get the desired result.
This works perfectly, and on iPad too.
So after hours of grueling work trying to use these current solutions (and utterly failing) I finally got things working well, and updated them to use the new animation blocks. My answer is entirely based on Ortwin's answer above.
So for whatever reason the code above was just not working for me. My setup seemed fairly similar to others, but maybe because I was on an iPad or 4.3... no idea. It was doing some wacky math and shooting my tableview off the screen.
See end result of my solution: http://screencast.com/t/hjBCuRrPC (Please ignore the photo. :-P)
So I went with the gist of what Ortwin was doing, but changed how it was doing some math to add up the origin.y & size.height of my table view with the height of the keyboard. When I subtract the height of the window from that result , it tells me how much intersection I have going on. If its greater than 0 (aka there is some overlap) I perform the animation of the frame height.
In addition there were some redraw issues that were solved by 1) Waiting to scroll to the cell until the animation was done and 2) using the UIViewAnimationOptionBeginFromCurrentState option when hiding the keyboard.
A couple things to note.
Again, I wouldn't have gotten near this answer if I Ortwin didn't provide the crux of it. Here's the code: