23

I am using UITextField to receive user inputs. However, since my textfields are towards the middle/bottom of the nib, it gets hidden when the keyboard pops up. Is there any way sort of slide it along with the keyboard so that it's on the top of the keyboard during selection? Also, since I am also using the numberpad, is there an easy way to include a done button somewhere?

Thanks for the help.

Toseef Khilji
  • 17,192
  • 12
  • 80
  • 121
intl
  • 2,753
  • 9
  • 45
  • 71
  • This is possibly a dupe of: http://stackoverflow.com/questions/1775860/uitextfield-move-view-when-keyboard-appears however I'm no iPhone dev so will bow out to the community to decide. – Kev Jun 23 '11 at 22:23
  • This may be a solution for you:- http://stackoverflow.com/a/17707278/1582217 – Mohd Iftekhar Qurashi Mar 22 '14 at 20:53

12 Answers12

27

I have made a simple Application for this problem. It automatically checked the Textfield's position and if keyboard hides it , it will automatically move up as per need.


- (void)textFieldDidBeginEditing:(UITextField *)textField
{
    [self animateTextField:textField up:YES];
}


- (void)textFieldDidEndEditing:(UITextField *)textField
{
    [self animateTextField:textField up:NO];
}


- (void) animateTextField: (UITextField*) textField up: (BOOL) up
{
    int animatedDistance;
    int moveUpValue = textField.frame.origin.y+ textField.frame.size.height;
    UIInterfaceOrientation orientation =
    [[UIApplication sharedApplication] statusBarOrientation];
    if (orientation == UIInterfaceOrientationPortrait ||
        orientation == UIInterfaceOrientationPortraitUpsideDown)
    {

        animatedDistance = 216-(460-moveUpValue-5);
    }
    else
    {
        animatedDistance = 162-(320-moveUpValue-5);
    }

    if(animatedDistance>0)
    {
        const int movementDistance = animatedDistance;
        const float movementDuration = 0.3f; 
        int movement = (up ? -movementDistance : movementDistance);
        [UIView beginAnimations: nil context: nil];
        [UIView setAnimationBeginsFromCurrentState: YES];
        [UIView setAnimationDuration: movementDuration];
        self.view.frame = CGRectOffset(self.view.frame, 0, movement);       
        [UIView commitAnimations];
    }
}

-(BOOL)textFieldShouldReturn:(UITextField *)textField {

    [textField resignFirstResponder];
    return YES;
}

ValayPatel
  • 1,094
  • 12
  • 15
  • I am in my 8th textfield & I clicked on tabbar, like a magic whole view got disappeared. why? – Dee Nov 17 '11 at 20:18
  • @Dee Please give me code.. I can review and will get back to you. – ValayPatel Nov 19 '11 at 21:04
  • If you are using this code use `[UIView transitionWithView...]` instead of `[UIView beginAnimations...],[UIView setAnimationBegins...],[UIView setAnimationDuration...],[UIView commitAnimations].` – DevC May 07 '14 at 11:31
  • This worked great - I really wanted to avoid using a ScrollView, and this did the trick. Since I am a n00b it did take me a little while to realize I had to set each textField.delegate = self to get the "textFieldDidBeginEditing" to fire. – deebs Nov 07 '14 at 20:21
  • This did work great for me initially (using Swift now), but when I touch a textField and the keyboard comes up, then I navigate to another screen before 'leaving' that textField... if I go back to the first screen the keyboard is still up. I then touch off the field and it moves negative distance and shows black space above the page. – deebs Nov 14 '14 at 21:23
18

To scroll when the keyboard appears, I like this tutorial from Cocoa With Love.

To dismiss the number keypad, you can put a custom "Done" button on the keypad or make an invisible button over the rest of the screen. I have done the latter with code, but this tutorial uses Interface Builder.

gerry3
  • 21,420
  • 9
  • 66
  • 74
3

Below code works perfect for me .

[textFieldFocused becomeFirstResponder];
[self.view addSubview:startView];
[textFieldFocused resignFirstResponder];
idea2real
  • 71
  • 6
3

You'll have to do this one manually. Check out this answer. UITextField: move view when keyboard appears

Community
  • 1
  • 1
Ben Zotto
  • 70,108
  • 23
  • 141
  • 204
3

if someone needs it, i`ve translated the solution of ValayPatel to swift

func animatedTextField(textField: UITextField, up: Bool){
    var animatedDistance : Int = 0
    let moveUpValue : Int = Int(textField.frame.origin.y) + Int(textField.frame.size.height)

    switch UIDevice.currentDevice().orientation {
    case .Portrait , .PortraitUpsideDown:
        animatedDistance = 216 - (460 - moveUpValue - 5)
    default:
        animatedDistance = 162 - (320 - moveUpValue - 5)
    }

    if (animatedDistance > 0 ){

        let movementDistance : Int = animatedDistance
        let movementDuration : Float = 0.3
        let movement = up ? -movementDistance : movementDistance
        UIView.beginAnimations(nil, context: nil)
        UIView.setAnimationBeginsFromCurrentState(true)
        UIView.setAnimationDuration(NSTimeInterval(movementDuration))
        self.view.frame = CGRectOffset(self.view.frame, 0, CGFloat(movement))
        UIView.commitAnimations()

    }

}
3

Use this code:

- (void)viewDidAppear:(BOOL)animated
{
[super viewDidAppear:animated];
// register for keyboard notifications
[[NSNotificationCenter defaultCenter] addObserver:self 
                                         selector:@selector(keyboardDidShow:) 
                                             name:UIKeyboardDidShowNotification 
                                           object:self.view.window];
[[NSNotificationCenter defaultCenter] addObserver:self 
                                         selector:@selector(keyboardDidHide:) 
                                             name:UIKeyboardDidHideNotification 
                                           object:self.view.window];
}


- (void)viewDidDisappear:(BOOL)animated {
// unregister for keyboard notifications while not visible.
[[NSNotificationCenter defaultCenter] removeObserver:self 
                                                name:UIKeyboardDidShowNotification 
                                              object:nil]; 
[[NSNotificationCenter defaultCenter] removeObserver:self 
                                                name:UIKeyboardDidHideNotification 
                                              object:nil];
}

- (void)keyboardDidHide:(NSNotification *)n
{
CGRect viewFrame = scrollView.frame;
UIDeviceOrientation orientSide = [[UIDevice currentDevice] orientation];
if ((orientSide == UIDeviceOrientationLandscapeRight) || (orientSide == UIDeviceOrientationLandscapeLeft)) 
    viewFrame.size.height += 140;
else viewFrame.size.height += 216; //portrait mode
scrollView.frame = viewFrame;
keyboardVisible = NO;
}

- (void)keyboardDidShow:(NSNotification *)n
{
CGRect viewFrame = scrollView.frame;
UIDeviceOrientation orientSide = [[UIDevice currentDevice] orientation];
if ((orientSide == UIDeviceOrientationLandscapeRight) || (orientSide == UIDeviceOrientationLandscapeLeft)) 
    viewFrame.size.height -= 140;
else viewFrame.size.height -= 216; //portrait mode
scrollView.frame = viewFrame;
keyboardVisible = YES; }

- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
[textField resignFirstResponder];
return YES;
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
CGRect viewFrame = scrollView.frame;
if ((interfaceOrientation == UIDeviceOrientationLandscapeRight) || (interfaceOrientation == UIDeviceOrientationLandscapeLeft)) //wants to change to landscape mode
    if (keyboardVisible == NO)//currently in portrait,check if keyboard was present
            viewFrame.size = CGSizeMake(480,250);
    else viewFrame.size = CGSizeMake(480,170);
else {//wants to change to portrait mode
    if (keyboardVisible == NO)//currently in landscape,check if keyboard was present
        viewFrame.size = CGSizeMake(320,416);
    else viewFrame.size = CGSizeMake(320,200);
}
scrollView.frame = viewFrame;

return YES;
}

Works for landscape mode too, but only for iphone. For iPad, change the frame settings accordingly. The textFieldShouldReturn: method will make the keyboard hide when return is pressed. Hope this helps...

tipycalFlow
  • 7,594
  • 4
  • 34
  • 45
  • What is scrollView parameter? – Dejell Sep 09 '12 at 20:07
  • It's the name of your scrollView that contains the textfields. – tipycalFlow Sep 10 '12 at 04:23
  • This can cause problems, because viewDidUnload will never get called in iOS 6 and further, so you actually do not unregister the listeners. – ljboy Dec 17 '13 at 08:22
  • @ljboy Good point. I've updated the answer to make it work across `iOS`es. But the accepted and other top answers are the way to go. I was just having a go at it back then :D – tipycalFlow Dec 17 '13 at 11:22
1

This situation kind of sucks on the iPhone. What you are supposed to do is either design your interface in such a way that the fields are visible when the keyboard appears, or shift the field up when the keyboard appears. (And back down when you are done)

I suggest to look at some Apple apps like Contacts or Settings to see how they are dealing with this.

Also, I'm sure the iPhone HIG has something to say about it.

Stefan Arentz
  • 34,311
  • 8
  • 67
  • 88
0

I know I'm a little late in answering this but, I found a very simple solution. If you use a UITableView controller rather than a UIViewController having a tableView as an object in it, this issue does not appear.

When you click the textfield that is at the bottom of the table, the keyboard pops up and the table view automatically scrolls up till the textfield that is being edited is visible.

However the auto scrolling does not work when we use the return button on the keyboard. In this scenario we have manually scroll the table up to see the textfield.

I hope this helps

0

I have been searching through countless answers to similar questions. For basic single screen apps, this solutions works like a charm and is incredibly simple to implement: https://github.com/michaeltyson/TPKeyboardAvoiding

Certainly there are more elegant solutions, but this will get the job done and quick.

Matt Perejda
  • 509
  • 5
  • 14
0

It's simple as setting UITableView to edition mode !

- (void)viewDidLoad {

    [super viewDidLoad];

    [self.tableView setEditing:YES];
}

If you would like to hide a delete bubbels, to the left of a cell then implement a UITableViewDelegate Method:

- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {

    return NO;
}
Blazej SLEBODA
  • 8,936
  • 7
  • 53
  • 93
0
- (void)textFieldDidBeginEditing:(UITextField *)textField
{
    NSLog(@"%f",textField.frame.origin.y);

    CGPoint scrollPoint = CGPointMake(0, textField.frame.origin);
    [scrollView setContentOffset:scrollPoint animated:YES];
}

- (void)textFieldDidEndEditing:(UITextField *)textField 
{
    [scrollView setContentOffset:CGPointZero animated:YES];
}
NightFury
  • 13,436
  • 6
  • 71
  • 120
0
- (void)textFieldDidBeginEditing:(UITextField *)textField
{
    [self animateTextField:textField up:YES];
}

Please use this code in your view controller.m file ..Using this code when you click the text field the keyboard will appear.Again when you click your view the keyboard will be hide ..I hope this is helps you...

Roby
  • 1
  • 1