scroll to a specific textfield in a scrollview/scrollview stop scrolling when keyboard is on screen

StackOverflow https://stackoverflow.com/questions/1987943

  •  22-09-2019
  •  | 
  •  

Question

I got stuck in here,

I have a custom scrollview in a view this scroll view has an add field button which lets user to add new text field to the scrollview. When user taps on a particular textfield keyboard appears and hide the textfield, to overcome this I followed UICatalog example, but it moves the whole scrollview up

to prevent this I followed UICatalog example and did this

-(void)textFieldDidBeginEditing:(UITextField *)sender
{

if (sender.frame.origin.y>109) {
    moveScrollViewUpBy=(sender.frame.origin.y-109+10);

    [self viewMovedUp:YES];
}
}


-(void)viewMovedUp:(BOOL)movedUp
{
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:0.3];

CGRect rect = formScrollView.frame;
if (movedUp)
{
    if(rect.origin.y == 0.0f){
        rect.origin.y -= kOFFSET_FOR_KEYBOARD;
        rect.size.height += kOFFSET_FOR_KEYBOARD;
    }
}
else
{
    if(rect.origin.y != 0.0f){
        rect.origin.y += kOFFSET_FOR_KEYBOARD;
        rect.size.height -= kOFFSET_FOR_KEYBOARD;
    }
}
self.formScrollView = rect;


[UIView commitAnimations];
}

here

#define kOFFSET_FOR_KEYBOARD    160.0

but this shifts the scroll view up,

I want that when i tap on a textfield down below in scroll view... scroll view scrolls and that textfield appears....

is it possible to do so....otherwise suggest me some other solution

I have one more query srollview just stop scrolling while the keyboard stays on screen. Is there any way to overcome this

Was it helpful?

Solution

You probably just want to set the scrollView's contentOffset property, rather than adjust its bounds.

OTHER TIPS

Implement the UITextViewDelegate and do the following:

- (void)textViewDidBeginEditing:(UITextView *)textView {

    keyboardShowing = YES; //helper ivar
    [self sizeToOrientation];
    [self.scrollView scrollRectToVisible:textView.frame animated:YES];
}

- (void)textFieldDidEndEditing:(UITextField *)textField {
    keyboardShowing = NO;
    [self sizeToOrientation];
}

Helper method:

//fit to the appropriate view sizes
- (void)sizeToOrientation {
    CGSize size;
    if( keyboardShowing )
        size = CGSizeMake(320, 190); //make room for keyboard
    else
        size = CGSizeMake(320, 420); //full height with tabbar on bottom

    self.scrollView.frame = CGRectMake(0, 0, size.width, size.height);
}

This works well for me.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top