Shifting UIView while UITextView is editing

962 Views Asked by At

I have a big comment field, which is a UITextView. Since the keyboard appears, and the UITextField is nearly half of the whole view, users can hardly see the field. What I want to do is when the keyboard appears, (i.e. the UITextView is editing) I want to shift the view up to make it completely visible, and user friendly.

2

There are 2 best solutions below

0
On BEST ANSWER

After looking through complicated and useless codes, I found a quick solution here:

https://stackoverflow.com/a/5221243/1139419

This answer here handles this situation very gently, thanks to Dan Ray

A quick note regarding: Calling this function not quite did the charm for me

-(void)scrollToView:(UIView *)view {
    CGRect theFrame = view.frame;
    float y = theFrame.origin.y - 15;
    y -= (y/1.7);
    [self scrollToY:-y]; 
}

so I edited this function:

-(void)scrollToView:(UIView *)view withCoefficient:(CGFloat)coeff
{
    CGRect theFrame = view.frame;
    float y = theFrame.origin.y;
    y -= (y/coeff);
    [self scrollToY:-y];
}

into this. This way I can try which coefficient for sliding my main view is proper. Instead of 1.7, 2 did the charm for me. Please visit the link for full solution. Also note that you might want to apply same thing for your UINavigationController as well, if exists;

-(void)textViewDidBeginEditing:(UITextView *)textView
{
    [self.view scrollToView:textView withCoefficient:2.0];
    [self.navigationController.view scrollToView:textView withCoefficient:2.0];
}
2
On

You can use the

UITextViewTextDidBeginEditing

to rearrange items in your view. And use

– bringSubviewToFront:

to bring the textview to the front.