Shubhank
Shubhank

Reputation: 21805

UITextView Not Scrolling after updating its frame

I have registered for notifications of Keyboard showing and Hiding.

The text View is scrollable when not clicked on it.. i.e. when it is not in editable mode.. When user click .. i update its frame to come up ..and after that it doesn't scroll.. after ending editing.. i change its position back its scrollable again..

Here is my code

- (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.NoteTextView.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.origin.y += (keyboardSize.height * 0.36);

    [UIView beginAnimations:nil context:NULL];
    [UIView setAnimationBeginsFromCurrentState:YES];
    // The kKeyboardAnimationDuration I am using is 0.3
    [UIView setAnimationDuration:0.1];

    [UIView commitAnimations];

    [self.NoteTextView setContentSize:CGSizeMake(310,580)];
    [self.NoteTextView setScrollEnabled:YES];

    keyboardIsShown = NO;
}

- (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
    CGSize keyboardSize = [[userInfo objectForKey:UIKeyboardFrameBeginUserInfoKey] CGRectValue].size;


    // resize the noteView
    CGRect viewFrame = self.NoteTextView.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.origin.y -= (keyboardSize.height * 0.36);

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


    [self.NoteTextView setContentSize:CGSizeMake(310, 580)];
    [self.NoteTextView setScrollEnabled:YES];
    keyboardIsShown = YES;
}

I have even enabled scrolling enabled and changed its content size in the methods.

Upvotes: 2

Views: 747

Answers (1)

It sound really weird.
My Suggestions:
1. Check that non of its superview's UserInteractioEnable property in the new frame isn't false
2. Try this code on new and clear project.

Upvotes: 1

Related Questions