0

I have a view having text field. Also configured the keyboard to have done button to hide the keyboard. I have attached delegate to my txtfield to scroll up when keyboard is shown. Its working fine but when i click on Done to hide the keyboard , the view remains scrolled up and user has to manually scroll it back.. any idea/pointer how to fix this..

Below is my code to acheive the scrolling up functionality..

- (void)viewWillAppear:(BOOL)animated
{
    [super viewDidAppear:animated];
    keyboardIsShown = NO;

    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(keyboardWillShow:)
                                                 name:UIKeyboardWillShowNotification
                                               object:self.view.window];
    // register for keyboard notifications
    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(keyboardWillHide:)
                                                 name:UIKeyboardWillHideNotification
                                               object:self.view.window];
}

- (void)viewWillDisappear:(BOOL)animated
{

    [super viewDidDisappear:animated];

    [[NSNotificationCenter defaultCenter] removeObserver:self
                                                    name:UIKeyboardWillShowNotification
                                                  object:nil];

    [[NSNotificationCenter defaultCenter] removeObserver:self
                                                    name:UIKeyboardWillHideNotification
                                                  object:nil];
}


- (void)keyboardWillShow:(NSNotification *)n
{
    if (keyboardIsShown) {
        return;
    }

    NSDictionary* userInfo = [n userInfo];

    // get the size of the keyboard
    CGSize keyboardSize = [[userInfo objectForKey:UIKeyboardFrameBeginUserInfoKey] CGRectValue].size;

    // resize the noteView
    CGRect viewFrame = self.scrollView.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.scrollView setFrame:viewFrame];
    [UIView commitAnimations];

    scrollView.contentSize = formView.frame.size;


    keyboardIsShown = YES;
}

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

    // get the size of the keyboard
    CGSize keyboardSize = [[userInfo objectForKey:UIKeyboardFrameBeginUserInfoKey] CGRectValue].size;


    // resize the scrollview
    CGRect viewFrame = self.scrollView.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.scrollView setFrame:viewFrame];
    [UIView commitAnimations];

    keyboardIsShown = NO;
}
Lalit_vicky
  • 295
  • 1
  • 5
  • 15
  • 1
    you could shorten your code by removing all observers that had been registered like this: [[NSNotificationCenter defaultCenter] removeObserver:self]; – Alex Cio Oct 07 '13 at 23:32

2 Answers2

0

If you just want to scroll back to the top with animation.

-(void)keyboardWillHide:(NSNotification *)notification {
        UIEdgeInsets contentInsets = UIEdgeInsetsZero;
        CGPoint top = CGPointMake(0, 0);
        [self.scrollView setContentOffset:top animated:YES];
        self.scrollView.scrollIndicatorInsets = contentInsets;
}

The keyboardIsShown instance variable is not needed in your above implementation

Kyle C
  • 4,077
  • 2
  • 31
  • 34
  • Thanks @kyle C.. i have added CGPoint top = CGPointMake(0, -65); and its working. Hope this hardcoding of Y co-ordinate will not create any prob ?? any suggestion plz. – Lalit_vicky Oct 08 '13 at 02:48
0

Try this:

- (void)textFieldDidBeginEditing:(UITextField *)textField
{
    UIEdgeInsets contentInsets = UIEdgeInsetsMake(0.0, 0.0, 216, 0.0);
    self.scrollView.contentInset = contentInsets;
    self.scrollView.scrollIndicatorInsets = contentInsets;
    CGRect frame = CGRectMake(0, textFieds.frame.origin.y + kOtherViewsHeightWhichMightNotBeInSameViewAsScrollView, 10, 10);

    [self.scrollView scrollRectToVisible:frame] animated:YES];
}

- (BOOL)textFieldShouldEndEditing:(UITextField *)textField
{
    UIEdgeInsets contentInsets = UIEdgeInsetsZero;
    self.scrollView.contentInset = contentInsets;
    self.scrollView.scrollIndicatorInsets = contentInsets;
    return YES;
}

Don't forget to set your view controller as the delegate of your text fields

Lucas
  • 6,675
  • 3
  • 25
  • 43