How to scroll view up when keyboard appears?

后端 未结 7 1558
小蘑菇
小蘑菇 2021-01-31 02:54

I know that this question has been asked over and over again, but nothing seems to be working for me. Most of the solutions around are pretty out of date, and the rest are incre

7条回答
  •  眼角桃花
    2021-01-31 03:24

    If you have a UITableView or a UIScrollView it's better to change values for contentOffset instead of making changes to the frame.

    Working on Peter's Answer, adding this method to your class works nicely:

    - (void)textViewDidBeginEditing:(UITextField *)textField {
        [UIView beginAnimations:nil context:NULL];
        [UIView setAnimationDuration:0.35f];
        CGPoint offset = self.tableView.contentOffset;
        offset.y += 200; // You can change this, but 200 doesn't create any problems
        [self.tableView setContentOffset:offset];
        [UIView commitAnimations];
    }
    

    That's it, no need to add the textViewDidEndEditing method.


    I shouldn't need to say this, but for this to work your UITextField or UITextView must be a delegate of your controller.

提交回复
热议问题