0

I have a form which has multiple textfields and textviews. When keyboad is shown I update scrollview frame to prevent textfields hiding under keyboard. I did follow this question and implement a similar solution.

It works with uitextfields actually but not for uitextviews.

When keyboard is shown scrollview height's is reduced as much as keyboard height and then scroll to focused uitextfield. But if the focused item is uitextview it just doesnt scroll while height of uiscrollview is reduced.

IB screenshots for my scrollview;

https://www.dropbox.com/s/a1iblh6nsdqy34t/Screenshot%202014-05-30%2021.44.32.png https://www.dropbox.com/s/o2h8avt5w82r5eo/Screenshot%202014-05-30%2021.45.19.png

Here is the code;

// register for keyboard notifications
[[NSNotificationCenter defaultCenter] addObserver:self
                                         selector:@selector(keyboardWillShow:)
                                             name:UIKeyboardWillShowNotification
                                           object:nil];

[[NSNotificationCenter defaultCenter] addObserver:self
                                         selector:@selector(keyboardDidShow:)
                                             name:UIKeyboardDidShowNotification
                                           object:nil];

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

[[NSNotificationCenter defaultCenter] addObserver:self
                                         selector:@selector(keyboardDidHide:)
                                             name:UIKeyboardDidHideNotification
                                           object:nil];

Notification handler;

    -(void)keyboardWillShow:(NSNotification *)n {

    if(keyboardUp)
        return;

    NSDictionary* userInfo = [n userInfo];

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


    const int movementDistance = -keyboardSize.height - keyboardPaddingToScrollView;
    const float movementDuration = 0.30f;

    int movement = movementDistance;

    [UIView beginAnimations: @"anim" context: nil];
    [UIView setAnimationBeginsFromCurrentState: YES];
    [UIView setAnimationDuration: movementDuration];

    CGRect contentSize = self.view.frame;
    contentSize.size.height += movementDistance;
    [self.view setFrame:contentSize];

    [UIView commitAnimations];

}

-(void)keyboardDidShow:(NSNotification *)n {

    keyboardUp = true;

    NSLog(NSStringFromCGRect(self.view.frame));
}

-(void)keyboardWillHide:(NSNotification *)n {

    if(!keyboardUp)
        return;

    NSDictionary* userInfo = [n userInfo];

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


    const int movementDistance = keyboardSize.height + keyboardPaddingToScrollView;
    const float movementDuration = 0.30f;

    int movement = movementDistance;

    [UIView beginAnimations: @"anim" context: nil];
    [UIView setAnimationBeginsFromCurrentState: YES];
    [UIView setAnimationDuration: movementDuration];

    CGRect contentSize = self.view.frame;
    contentSize.size.height += movementDistance;
    self.view.frame = contentSize;

    [UIView commitAnimations];


}

-(void)keyboardDidHide:(NSNotification *)n {

    keyboardUp = false;
}
Community
  • 1
  • 1
Fatih Donmez
  • 4,319
  • 3
  • 33
  • 45
  • Do you have your text fields/ text views added to uiscrollview? – Greg May 30 '14 at 18:09
  • @Greg I edited the question for screenshots you can check. – Fatih Donmez May 30 '14 at 18:46
  • UITextView is a scroll view. So you have two nested scroll views squeezed above the keyboard, both of which scroll vertically (potentially)? How are you going to determine which scroll view the user is trying to scroll? – bilobatum May 31 '14 at 08:18
  • Yeap I think you're right because after i replaced uitextviews with uitextfields, it start to work as expected. I absolutely noidea how to solve your point. Any idea? @bilobatum – Fatih Donmez May 31 '14 at 08:37
  • 1
    @tylerdurden Web sites solve this problem by providing a buffer to the left and right of the text view. If the mouse cursor is outside the text view and inside the buffer, scrolling is interpreted as scrolling the page content. If the mouse cursor is inside the text view, scrolling is interpreted as scrolling the text (the text view I'm typing in right now exhibits this behavior). This technique, however, doesn't translate well to a small screen. On the iPhone, I think it's best not to nest a text view inside another scroll view if the outer scroll view scrolls vertically. – bilobatum May 31 '14 at 23:12
  • I changed the ux actually. I looked for other major app, most of them getting big textview data with another dedicated view controller by pushing or presenting modal. Your comment make me think that if i prevent the uitextview focus first, then scroll after that refocus on textview can be tricky solution. – Fatih Donmez Jun 01 '14 at 07:52

0 Answers0