Reputation: 26476
I have a small form which I built mostly in interface builder:
When the user touches inside one of the textfields, I make the top part of the screen scrollable (note that it does not require any scrolling when the keyboard is not present, and that neither field is hidden behind the keyboard).
I achieve this with very little code. My form controls are all inside a UIScrollView, which is disabled and sized 0x0 (by default) on load. When I detect the keyboard's presence I enable it and resize it to the views's width and the view's height + the keyboard size. I then disable and resize the scrollview when the keyboard is removed (I set it back to 0x0).
This works almost perfectly. With one exception (so far):
The scrollable area (or rather, the view inside it) is too tall. Because the form without the keyboard does not take up the whole screen, I find that I can scroll to the bottom of my view (one full screen) + keyboard height, when what I really want is to scroll to the view's contents + keyboard height.
I thought I could simply make the view shorter so that it is just the right size for the contents, but interface builder (xcode 4.2) seems to force it to take up the whole screen. Attempting to do it in code also doesn't work, because apparently self.view.frame.size is not assignable. How can I achieve what I'm looking for? Specifically, when the keyboard is present, I want the scrollable area in the top half of the screen to be sized such that there isn't so much 'slack' at the bottom:
Upvotes: 0
Views: 1843
Reputation: 321
First, self.view.frame is a struct with two fields that are structs itself - size and origin. If you want to change either size or origin, you have change the whole frame
self.view.frame = CGRectMake(self.view.frame.origin.x,self.view.frame.origin.y,
self.view.frame.size.width, <your desired height>);
Second, what base do use to create your form? This looks like just a UITableVIew. If so, consider changing type of your UIViewController to UITableViewController, since it handles keyboard automatically.
If neither is an option for you, provide the code you use when handling keyboard appearance, so that we could think of more concrete answer.
Upvotes: 1