0

I have an app where you have textfields and one textview but when I get the keyboard it hides the lower textfields. How would I do it.

I have tried:

.m:

- (void)textFieldDidBeginEditing:(UITextField *)sender {
    CGSize content = _scrollView.contentSize;
    _scrollView.contentSize = CGSizeMake(content.width, content.height + 200);
    svos = _scrollView.contentOffset;
    CGPoint pt;
    CGRect rc = [sender bounds];
    rc = [sender convertRect:rc toView:_scrollView];
    pt = rc.origin;
    pt.x = 0;
    pt.y -= 200;
    [_scrollView setContentOffset:pt animated:YES];
}

- (IBAction)textFieldShouldReturn:(UITextField *)textField {
    CGSize content = _scrollView.contentSize;
    _scrollView.contentSize = CGSizeMake(content.width, content.height - 200);
    [_scrollView setContentOffset:svos animated:YES];
    [textField resignFirstResponder];
}

.h:

CGPoint svos;

Although the bottom text fields are still hidden it does scroll to the visible ones

  • What you want to achieve is not so straightforward. You have obtained the origin of the sender textfield but only move up by 60, thus, the lower textfields are covered by the keyboard. You will need to know the height of the keyboard and calculate the distance to move up. Check this out [http://stackoverflow.com/questions/1126726/how-to-make-a-uitextfield-move-up-when-keyboard-is-present?rq=1](http://stackoverflow.com/questions/1126726/how-to-make-a-uitextfield-move-up-when-keyboard-is-present?rq=1) – Rick Jul 13 '13 at 14:09
  • @Rick That works apart from that when you change which item you are editing the app crashes and if the content is to low it can't be scrolled to –  Jul 13 '13 at 14:14
  • You are using a scrollview, you can extend its contentSize with blank space in order to scroll to the bottom textfield. As for why the app crashed, you will need to elaborate. – Rick Jul 13 '13 at 14:23
  • @Rick How would I add blank space there is no room in my xib??? –  Jul 13 '13 at 14:32

3 Answers3

0

You have obtained the origin of the sender textfield but only move up by 60, thus, the lower textfields are covered by the keyboard. You will need to know the height of the keyboard and calculate the distance to move up. Check this out. It has much of the answer so I will not explain again.

To scroll to the bottom textfield inside a scrollview, add these lines in textFieldDidBeginEditing:

CGSize content = _scrollview.contentSize;
_scrollview.contentSize = CGSizeMake(content.width, content.height + 200);

This will extend your contentSize programmatically so you can scroll to the last textfield and allow the keyboard to cover the empty space.

In textFieldDidEndEditing or textFieldShouldReturn, in your case, add these:

CGSize content = _scrollview.contentSize;
_scrollview.contentSize = CGSizeMake(content.width, content.height - 200);

I used an arbitrary 200 as example. You will need to figure out how much you want.

Community
  • 1
  • 1
Rick
  • 1,818
  • 1
  • 15
  • 18
  • It is not adding the px on the bottom –  Jul 13 '13 at 14:53
  • Please elaborate. Also, try setting your `pt.y -= 60;` to `pt.y -= 200;` and see if there is any changes. – Rick Jul 13 '13 at 14:59
  • Yup. This line `[sender convertRect:rc toView:_scrollView]`. Are your textfields on a UIView, which is a subview of the scrollview? – Rick Jul 13 '13 at 15:30
  • No they are just on the scrollview –  Jul 13 '13 at 15:35
  • It doesn't like the line you sent. It says 'use of undeclared identifier rc' how would I set it and to what –  Jul 13 '13 at 15:37
  • In that case, you do not need this line at all since the origin of the textfields are with respect to the scrollview. Anyway, it shouldn't be 'sender'. By the way, are you using the scrollview just to scroll up the textfields or because your content couldn't fit into the screen? – Rick Jul 13 '13 at 15:39
  • This is getting too lengthy, do read the link that I had pointed to you and understand what you are doing before we continue. There may be other problems that you need to resolve first before this. And that includes the protocol and delegate declarations that I highlighted to you in the other earlier post. – Rick Jul 13 '13 at 15:46
  • Yeah I am using the scroll view as I am adding a navbar programatically and some elements can't be seen –  Jul 13 '13 at 15:48
0

A drop-in universal solution for moving text fields out of the way of the keyboard in iOS

https://github.com/michaeltyson/TPKeyboardAvoiding

It works perfect for me.

Simply you need to copy the required classes(TPKeyboardAvoidingScrollView or TPKeyboardAvoidingTableView) in your project and in your interface builder file you have to change the UIScrollView class to TPKeyboardAvoidingScrollView or UITableView to TPKeyboardAvoidingTableView, the remaining things will be handled by these classes.

enter image description here

Muhammad Zeeshan
  • 2,441
  • 22
  • 33
0

Have you seen the documentation about managing the keyboard? (apple documentation) There is an example of what i think you are working on. Hope it helps.

Javito_009
  • 115
  • 6