How to make an UITextView scrollable when the keyb

2020-05-01 08:12发布

问题:

I would like to know how I can make my UITextView scrollable when it's being edited ? When the user wants to edit it, the keyboard shows but then the UITextView is no more scrollable. So all the text behind the keyboard is not visible.

回答1:

You can reduce sizeOf you textView on keyBoard appear

-(void)textViewDidBeginEditing:(UITextView *)textView
{
    CGRect frame = txtAddNote.frame;
    frame.size.height = 150; //Decrease your textView height at this time
    txtAddNote.frame = frame;
}
-(IBAction)DoneBarBtnPress:(id)sender
{
    CGRect frame = txtAddNote.frame;
    frame.size.height = 212; //Original Size of textView
    txtAddNote.frame = frame;

    //Keyboard dismiss 
    [self.view endEditing:YES];
}


回答2:

just scroll the view when UITextView begin edit like this..

-(void)textViewDidBeginEditing:(UITextView *)textView
{
    [UIView beginAnimations:nil context:nil];
    [UIView setAnimationDuration:0.3];
    self.view.frame = CGRectMake(self.view.frame.origin.x, -160, self.view.frame.size.width, self.view.frame.size.height);
    [UIView commitAnimations];

}

and in endEditing just set Default screen like bellow..

-(void)textViewDidEndEditing:(UITextView *)textView
{
    [textView resignFirstResponder];
    [UIView beginAnimations:nil context:NULL];
    [UIView setAnimationDuration:0.3];
    self.view.frame = CGRectMake(self.view.frame.origin.x, 0, self.view.frame.size.width, self.view.frame.size.height);
    [UIView commitAnimations];
}

UPDATE

Here with your requirement we set the frame with our view see bellow code

-(void)textViewDidBeginEditing:(UITextView *)textView
    {
        [UIView beginAnimations:nil context:nil];
        [UIView setAnimationDuration:0.3];
         [yourTextView setFrame:CGRectMake(self.view.frame.origin.x,self.view.frame.origin.y,self.view.‌​frame.size.width,self.view.frame.size.height - 160)];
        [UIView commitAnimations];

    }

-(void)textViewDidEndEditing:(UITextView *)textView
    {
        [textView resignFirstResponder];
        [UIView beginAnimations:nil context:NULL];
        [UIView setAnimationDuration:0.3];
        [yourTextView setFrame:self.view];
        [UIView commitAnimations];
    }