Skip to content

Instantly share code, notes, and snippets.

@benzamin
Created May 14, 2017 08:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save benzamin/fc96755dd0f985f99ab80850993686d8 to your computer and use it in GitHub Desktop.
Save benzamin/fc96755dd0f985f99ab80850993686d8 to your computer and use it in GitHub Desktop.
Automatic view pushing when keyboard covers a text field depending on the position of the textfield in the window.
#import <UIKit/UIKit.h>
@interface MyViewController : UIViewController
@end
#import "MyViewController.h"
@interface MyViewController () <UITextFieldDelegate>
@property(nonatomic, weak) IBOutlet UITextField *txtDescription;
@property(nonatomic, assign) CGFloat viewMoveUpOffsetForKeyboard; //for keeping track of the keyboard's view push height
@end
@implementation MyViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
self.txtDescription.delegate = self;
}
#pragma mark - UITextFieldDelegate
#define AVERAGE_KEYBOARD_HEIGHT 260 //Average iOS keyboard height in pixels, for both iPhone SE/5S/5 and 6/6s/7/7s/6S+/7S+
-(BOOL)textFieldShouldReturn:(UITextField *)textField
{
if (self.view.frame.origin.y < 0)
{
[self setViewMovedUp:NO];
self.viewMoveUpOffsetForKeyboard = 0;
}
[textField resignFirstResponder];
return NO;
}
-(void)textFieldDidBeginEditing:(UITextField *)textField
{
float screenHeight = [UIScreen mainScreen].bounds.size.height; //height of the device screen
CGPoint textFieldXYPointInWindow = [textField convertPoint:CGPointMake(0, 0) toView:nil]; //current textfield's (x,y) point in window
float textFieldBottomPoint = textFieldXYPointInWindow.y + textField.frame.size.height; //bottom point of the text field
if (screenHeight - textFieldBottomPoint < AVERAGE_KEYBOARD_HEIGHT) //is the textfield under keyboard?
{
self.viewMoveUpOffsetForKeyboard = AVERAGE_KEYBOARD_HEIGHT - (screenHeight - textFiledBottomPoint) + 30; //this 30 is just an offset
if(self.view.frame.origin.y >= 0)
[self setViewMovedUp:YES];
}
}
-(void)textFieldDidEndEditing:(UITextField *)textField
{
if (self.view.frame.origin.y < 0) //already view moved up? Bring it down
{
[self setViewMovedUp:NO];
}
}
//method to move the view up/down whenever the keyboard is shown/dismissed
-(void)setViewMovedUp:(BOOL)movedUp
{
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:0.3]; // if you want to slide up the view
CGRect rect = self.view.frame;
if (movedUp)
{ // 1. move the view's origin up so that the text field that will be hidden come above the keyboard
// 2. increase the size of the view so that the area behind the keyboard is covered up.
rect.origin.y -= self.viewMoveUpOffsetForKeyboard;
rect.size.height += self.viewMoveUpOffsetForKeyboard;
}
else
{ // revert back to the normal state.
rect.origin.y += self.viewMoveUpOffsetForKeyboard;
rect.size.height -= self.viewMoveUpOffsetForKeyboard;
}
self.view.frame = rect;
[UIView commitAnimations];
}
#pragma mark - end of UITextFieldDelegate
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment