background preloader

Divers tuto / à trier

Facebook Twitter

On iPhone UITextField auto-scroll « The Loop. During my early days of iPhone development, I came across this specific problem: How the heck do I auto-scroll the text fields behind the keyboard? Here’s a visual of what I mean. Suppose I have a text field named Rate as shown on image above. As soon as I click on this field, it will automatically launch the system keyboard of iPhone. iPhone’s keyboard launches from the bottom of screen going up, so it will block my view of the Rate field while I’m typing on it. Hence, I wouldn’t be able to see what I am typing. What I would like to do is to come up with an implementation of allowing the Rate text field to scroll up, once the keyboard is launched.

To achieve this effect, we need to define a UIScrollView and put our view inside it and scroll it up when the keyboard shows up. Next is to call on registerForKeyboardNotifications on the viewDidLoad or viewWillAppear event. - (void)viewDidLoad { [self registerForKeyboardNotifications]; [super viewDidLoad]; } Enjoy! Like this: Like Loading... Iphone - How to make a UITextField move up when keyboard is present. Iphone - UITableView and keyboard scrolling issue. Iphone - UITableView and keyboard scrolling issue. Sliding UITextFields around to avoid the keyboard. Hidden text fields The iPhone's onscreen keyboard occupies the bottom 216 pixels on screen (162 in landscape mode). That's around half the screen, so if you ever have a text field you want to edit in the bottom half of the screen, it needs to move or it will get covered.

You can just animate the whole window upwards by the height of the keyboard when editing a text field in the bottom half but this doesn't work well for text fields in the middle (they can get moved too far up). Instead, I'm going to show you a method which divides the window as follows: Everything in the top section will stay still when edited. Everything in the middle section will animate upwards by a fraction of the keyboard's height (proportional to the field's height within the middle section).

Everything in the bottom section will animate upwards by the keyboard's full height. Implementing the delegate methods Your view controller will need the following instance variable: Animate upwards when the text field is selected.