Search code examples
iosobjective-ckeyboarduitapgesturerecognizerfirst-responder

keyboard hides in between while entering text into textfield


i am working on a login screen which contain a scroll view and on the scrollview there are two text fields with a login button.

scrollview used to adjust for the iphone 5 screen size. and i am using a "tab gesture" so the if any user is entering text in text field and want to hide keyboard then can click on anywhere on the screen to hide key board. function used for the tab gesture is

- (void)viewDidLoad {

NSLog(@"login view");

UITapGestureRecognizer *singleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(singleTapGestureCaptured:)];
[self.scrollView addGestureRecognizer:singleTap]; }

- (void)singleTapGestureCaptured:(UITapGestureRecognizer *)gesture
{
[self.view endEditing:YES]; }

My problem is that when a user is entering text in the textfields using keyboard then in the middle of entering text keyboard detects the tab gesture and hides the keyboard in the middle.

what i did to solve the issue:- 1.) i changed the [self.view addGestureRecognizer:singleTap];

2.) i placed a view on the top of the screen with dimention (0,0,360,400) and apply the gesture to this view so that click on this view will hide keyboard but still when user types keyboard hide by calling gesture method

3.) i also used a button on the scrollview of half of screen size so taht user can click anywhere to hide the keybaord butsill while typing even then keyboard hides y calling the ibaction method of button palced


Solution

  • Use This to hide the keyboard and show the keyboard:-

    - (BOOL)textFieldShouldBeginEditing:(UITextField *)textField
    {
        if (textField == username)
        {
            [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillShow:) name:UIKeyboardDidShowNotification object:nil];
    
        }
        if (textField == password)
        {
            [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillShow:) name:UIKeyboardDidShowNotification object:nil];
    
        }
    
        return YES;
    }
    
    - (BOOL)textFieldShouldEndEditing:(UITextField *)textField
    {
        if (textField == username)
        {
            [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillHide:) name:UIKeyboardDidHideNotification object:nil];
        }
    
        if (textField == password)
        {
            [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillHide:) name:UIKeyboardDidShowNotification object:nil];
    
        }
        return YES;
    }
    
    
    - (void)keyboardWillShow:(NSNotification *)notification
    {
        CGSize keyboardSize = [[[notification userInfo] objectForKey:UIKeyboardFrameBeginUserInfoKey] CGRectValue].size;
    
        float newVerticalPosition = -keyboardSize.height + 100;
    
        [self moveFrameToVerticalPosition:newVerticalPosition forDuration:0.3f];
    }
    
    
    - (void)keyboardWillHide:(NSNotification *)notification
    {
        CGFloat  kNavBarHeight =  self.navigationController.navigationBar.frame.size.height;
        [self moveFrameToVerticalPosition:kNavBarHeight forDuration:0.3f];
    }
    
    - (void)moveFrameToVerticalPosition:(float)position forDuration:(float)duration
    {
        CGRect frame = self.view.frame;
        frame.origin.y = position;
    
        [UIView animateWithDuration:duration animations:^{
            self.view.frame = frame;
        }];
    }