When textfield start editing Keyboard will show and view go to upside after when click on view keyboard will hide view go to down side

UIScrollView, UITextField, NSNotification, UIGestureRecognizer, PassData

<UITextFieldDelegate>

@property (weak, nonatomic) IBOutlet UITextField *textFieldCurrent;

@property (weak, nonatomic) IBOutlet UITextField *textFieldFirstName;

@property (weak, nonatomic) IBOutlet UITextField *textFieldLastName;

@property (weak, nonatomic) IBOutlet UITextField *textFieldPhoneNumber;

@property (weak, nonatomic) IBOutlet UITextField *textFieldCity;

@property (weak, nonatomic) IBOutlet UIScrollView *scrollViewSignUp;

– (void)viewDidLoad {

    [super viewDidLoad];

    

    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillShow:) name:UIKeyboardWillShowNotification object:nil];

    

    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillHide:) name:UIKeyboardWillHideNotification object:nil];

    

    

    UITapGestureRecognizer *aGest = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapDetected)];

    aGest.numberOfTapsRequired = 1;

    [self.view addGestureRecognizer:aGest];

    // Do any additional setup after loading the view, typically from a nib.

}

-(void)viewWillDisappear:(BOOL)animated

{

    [super viewWillDisappear:animated];

    

    [[NSNotificationCenter defaultCenter] removeObserver:self

                                                    name:UIKeyboardWillShowNotification

                                                  object:nil];

    

    [[NSNotificationCenter defaultCenter] removeObserver:self

                                                    name:UIKeyboardWillHideNotification

                                                  object:nil];

}

#pragma mark – Gester method

– (void)tapDetected {

    

    [self.textFieldCurrent resignFirstResponder];

    

}

#pragma mark – UIKeyboard show/hide notification

– (void)keyboardWillShow:(NSNotification *)iNotification {

    

    NSLog(@”%f”, [iNotification.userInfo[UIKeyboardFrameEndUserInfoKey] CGRectValue].size.height);

    

    NSDictionary *aKeyInfo = iNotification.userInfo;

    NSValue *aRectValue = [aKeyInfo valueForKey:UIKeyboardFrameBeginUserInfoKey];

    CGRect aRect = [aRectValue CGRectValue];

    

    if (self.textFieldCurrent.tag == 3 || self.textFieldCurrent.tag == 4)

    {

        self.scrollViewSignUp.contentOffset = CGPointMake(0, (aRect.size.height) – ([UIScreen mainScreen].bounds.size.height / 5.3));

    }

    

}

– (void)keyboardWillHide:(NSNotification *)iNotification  {

    

    self.scrollViewSignUp.contentOffset = CGPointMake(0, 0);

    

}

#pragma mark – UITextField delegate

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

    

    [textField resignFirstResponder];

    return YES;

    

}

– (void)textFieldDidBeginEditing:(UITextField *)textField {

    

    self.textFieldCurrent = textField;

    

}

Click here to download When textfield start editing Keyboard will show and view go to upside after when click on view keyboard will hide view go to down side Sample Project

Leave a comment