How to Move UITextField Up When Keyboard Appears on iOS

How to Move UITextField Up When Keyboard Appears on iOS

22 May 2024 Stephan Petzl Leave a comment Tech-Help

When developing an iOS application, it’s common to encounter a situation where the keyboard covers a UITextField, making it difficult for users to see what they are typing. This article provides a step-by-step guide on how to move the UITextField up when the keyboard appears.

Understanding the Problem

You have a UIView with several UITextFields. When a UITextField becomes active, the keyboard appears, potentially obscuring the UITextField. The goal is to ensure that the UITextField remains visible by moving the view up.

Solution Overview

The solution involves adjusting the view’s position when the keyboard appears and disappears. We will use keyboard notifications to detect when the keyboard is shown or hidden and then animate the view accordingly.

Step-by-Step Implementation

1. Register for Keyboard Notifications

In your view controller, register for keyboard notifications in the viewWillAppear method and unregister in the viewWillDisappear method.

-(void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    [[NSNotificationCenter defaultCenter] addObserver:self 
                                             selector:@selector(keyboardWillShow:) 
                                                 name:UIKeyboardWillShowNotification 
                                               object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self 
                                             selector:@selector(keyboardWillHide:) 
                                                 name:UIKeyboardWillHideNotification 
                                               object:nil];
}

-(void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];
    [[NSNotificationCenter defaultCenter] removeObserver:self 
                                                    name:UIKeyboardWillShowNotification 
                                                  object:nil];
    [[NSNotificationCenter defaultCenter] removeObserver:self 
                                                    name:UIKeyboardWillHideNotification 
                                                  object:nil];
}

2. Handle Keyboard Notifications

Implement the methods to handle the keyboard notifications. These methods will adjust the view’s frame to ensure the active UITextField is visible.

#define kOFFSET_FOR_KEYBOARD 80.0

-(void)keyboardWillShow:(NSNotification *)notification {
    [self setViewMovedUp:YES];
}

-(void)keyboardWillHide:(NSNotification *)notification {
    [self setViewMovedUp:NO];
}

-(void)setViewMovedUp:(BOOL)movedUp {
    [UIView beginAnimations:nil context:NULL];
    [UIView setAnimationDuration:0.3];

    CGRect rect = self.view.frame;
    if (movedUp) {
        rect.origin.y -= kOFFSET_FOR_KEYBOARD;
        rect.size.height += kOFFSET_FOR_KEYBOARD;
    } else {
        rect.origin.y += kOFFSET_FOR_KEYBOARD;
        rect.size.height -= kOFFSET_FOR_KEYBOARD;
    }
    self.view.frame = rect;

    [UIView commitAnimations];
}

3. Adjust the Active UITextField

Ensure that the active UITextField is moved up when it begins editing.

-(void)textFieldDidBeginEditing:(UITextField *)sender {
    if ([sender isEqual:self.mailTextField]) {
        [self setViewMovedUp:YES];
    }
}

-(void)textFieldDidEndEditing:(UITextField *)sender {
    [self setViewMovedUp:NO];
}

Conclusion

By following these steps, you can ensure that UITextFields remain visible when the keyboard appears. This improves the user experience by allowing users to see what they are typing.

Enhance Your Mobile Development with Repeato

Automating the testing of your mobile applications can save significant time and effort. Repeato is a no-code test automation tool for iOS and Android that helps you create, run, and maintain automated tests for your apps. With its computer vision and AI capabilities, Repeato ensures your tests are accurate and reliable. It allows developers to focus on creating great products while enabling non-technical team members to handle test automation.

Learn more about how Repeato can streamline your mobile testing process on our documentation page.

Like this article? there’s more where that came from!