0
votes

i have a view where i added the text fields in scroll view. Scrolling is fine but when i want to resign the keyboard after writing text in any of text field then i am not able to do that !! i need to resign keyboard as soon as i click on anywhere in view except text field. some one suggested me to write a bunch of code:

- (void)keyboardWillShow:(NSNotification *)n
{
    // This is an ivar I'm using to ensure that we do not do the frame size adjustment on the UIScrollView if the keyboard is already shown.  This can happen if the user, after fixing editing a UITextField, scrolls the resized UIScrollView to another UITextField and attempts to edit the next UITextField.  If we were to resize the UIScrollView again, it would be disastrous.  NOTE: The keyboard notification will fire even when the keyboard is already shown.
    if (keyboardIsShown) {
        return;
    }

    NSDictionary* userInfo = [n userInfo];

    // get the size of the keyboard
    NSValue* boundsValue = [userInfo objectForKey:UIKeyboardBoundsUserInfoKey];
    CGSize keyboardSize = [boundsValue CGRectValue].size;

    // resize the noteView
    CGRect viewFrame = self.scroll.frame;
    // I'm also subtracting a constant kTabBarHeight because my UIScrollView was offset by the UITabBar so really only the portion of the keyboard that is leftover pass the UITabBar is obscuring my UIScrollView.
    viewFrame.size.height -= (keyboardSize.height - kTabBarHeight);

    [UIView beginAnimations:nil context:NULL];
    [UIView setAnimationBeginsFromCurrentState:YES];
    // The kKeyboardAnimationDuration I am using is 0.3
    [UIView setAnimationDuration:kKeyboardAnimationDuration];
    [self.scroll setFrame:viewFrame];
    [UIView commitAnimations];

    keyboardIsShown = YES;
}


- (void)keyboardWillHide:(NSNotification *)n
{  
    NSDictionary* userInfo = [n userInfo];

    // get the size of the keyboard
    NSValue* boundsValue = [userInfo objectForKey:UIKeyboardBoundsUserInfoKey];
    CGSize keyboardSize = [boundsValue CGRectValue].size;

   // resize the scrollview
    CGRect viewFrame = self.scroll.frame;

    /*I'm also subtracting a constant kTabBarHeight because my UIScrollView was offset by the UITabBar so really only the portion of the keyboard that is leftover pass the UITabBar is obscuring my UIScrollView.*/

    viewFrame.size.height += (keyboardSize.height - kTabBarHeight);
    [UIView beginAnimations:nil context:NULL];
    [UIView setAnimationBeginsFromCurrentState:YES];
    // The kKeyboardAnimationDuration I am using is 0.3
    [UIView setAnimationDuration:kKeyboardAnimationDuration];
    [self.scroll setFrame:viewFrame];
    [UIView commitAnimations];

    keyboardIsShown = NO;

}

i guess it will not provide me what i want . help plz

4

4 Answers

1
votes

you need to catch the touch event for others view (except your text field), you could do so by implementing touchesBegan method of UIResponder.

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

(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    UITouch * touch = [touches anyObject];
    [myTextField resignFirstResponder];
}
0
votes


Try below code

- (IBAction)backgroundTap:(id)sender 
{ 
     [yourtextfield resignFirstResponder];
}

In XIB attach this method with Touch Down event of UIView.

0
votes

One simple thing u can do is, Have a UIButton which covers your entire view and connect that button to the method mentioned by @dks1725.

-1
votes

You have 2 options

In viewDidLoad

UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(dismissKeyboard)];    
[self.view addGestureRecognizer:tap];

In dismissKeyboard: function

-(void)dismissKeyboard {
       [aTextField resignFirstResponder];
}

(Where aTextField is the textfield that is responsible for the keyboard)

OPTION 2

If you can't afford to add a gestureRecognizer then you can try this

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
    UITouch * touch = [touches anyObject];
    if(touch.phase == UITouchPhaseBegan) {
        [aTextField resignFirstResponder];
    }
}

All Credit goes to this answer.

Hope it helps.