View Up Down for keyboard show Hide

<UITextFieldDelegate>

@property (strong, nonatomic) UITextField *textFieldActive;

    UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self

                                                                                 action:@selector(handleTapTapGestureRecognizerEvent:)];

    [self.view addGestureRecognizer:tapGesture];

#pragma mark – TextField Delegate

– (BOOL)textFieldShouldBeginEditing:(UITextField *)textField

{

    // return NO to disallow editing.

    return YES;

    

}

– (void)textFieldDidBeginEditing:(UITextField *)textField

{

    // became first responder

    _textFieldActive = textField;

    if (_textFieldActive != _textFieldReferalID && _textFieldActive != _textFieldMailingAddress) {

        //    iPhone = -210

        //    iPad = -230

        [self viewUpDownWithAnimationYCoordinate:-230];

    }

    

}

– (BOOL)textFieldShouldEndEditing:(UITextField *)textField

{

    // return YES to allow editing to stop and to resign first responder status. NO to disallow the editing session to end

    return YES;

}

– (void)textFieldDidEndEditing:(UITextField *)textField

{

    // may be called if forced even if shouldEndEditing returns NO (e.g. view removed from window) or endEditing:YES called

    [_textFieldActive resignFirstResponder];

    [self viewUpDownWithAnimationYCoordinate:0];

}

– (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string

{

    // return NO to not change text

    return YES;

    

}

– (BOOL)textFieldShouldClear:(UITextField *)textField

{

    // called when clear button pressed. return NO to ignore (no notifications)

    return NO;

}

– (BOOL)textFieldShouldReturn:(UITextField *)textField

{

    // called when ‘return’ key pressed. return NO to ignore.

    [_textFieldActive resignFirstResponder];

    return YES;

}

– (void)viewUpDownWithAnimationYCoordinate:(float)yCoordinare

{

    [UIView beginAnimations:nil context:NULL];

    [UIView setAnimationDuration:0.35f];

    CGRect frame = self.view.frame;

    frame.origin.y = yCoordinare;

    [self.view setFrame:frame];

    [UIView commitAnimations];

    

}

#pragma mark –

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event

{

    [_textFieldActive resignFirstResponder];

    [self.view endEditing:YES];// this will do the trick

    

}

– (void)handleTapTapGestureRecognizerEvent:(UITapGestureRecognizer *)recognizer

{

    [_textFieldActive resignFirstResponder];

    

}

Leave a comment