2

I have a little problem with uitextfield & uiscrollview, the problem is that when I click on uitextfield the keyboard hide the field, for solve this I use:

- (BOOL) textFieldShouldBeginEditing:(UITextField *)textField {

    CGRect viewFrame = self.scroll.frame;
    viewFrame.size.height -= 186;
    [self.scroll setFrame:viewFrame];

    return YES;
}

- (BOOL) textFieldShouldReturn:(UITextField *)textField{

    [self.scroll setFrame:CGRectMake(0, 0, 320, 480)];

    [self inserisciField];

    [textField resignFirstResponder];

    return YES;
}

and all work, the problem is with multiple field, with a field selected if I change the field without click return the scroll view has a wrong size/positon but if I click return, then I Select a field, press return, change field, ecc...all work.

I think that there is this problem because If I change field the scroll dimension was changed two time (the first time view frame.size.height -= 186, and another time with -=186) then I try to put this method

- (BOOL) textFieldShouldEndEditing:(UITextField *)textField {

    [self.scroll setFrame:CGRectMake(0, 0, 320, 480)];

    return YES;
}

where I set the default size of frame, but nothing.

francesco.venica
  • 1,703
  • 2
  • 19
  • 54
  • Please, have a look at this S.O. article: http://stackoverflow.com/questions/1126726/how-to-make-a-uitextfield-move-up-when-keyboard-is-present – sergio Nov 10 '11 at 20:21
  • I try this method but the scrollview slide up only when I edit the field, not when I touch the field. Is normal? – francesco.venica Nov 10 '11 at 21:39

2 Answers2

3

use this -

- (BOOL) textFieldShouldBeginEditing:(UITextField *)textField {

    CGPoint point = CGPointMake(scroll.frame.origin.x, scroll.frame.origin.y + 186);
    [self.scroll setContentOffset:point];

    return YES;
}

- (BOOL) textFieldShouldEndEditing:(UITextField *)textField {

    CGPoint point = CGPointMake(0, 0);
    [self.scroll setContentOffset:point];
}
Akshay Nalawade
  • 1,447
  • 2
  • 14
  • 29
0

You can use the contentoffset property of scrollview when u touch or edit a textfield as it is the best solution for your problem and touch and edit are the same process - only when you touch the field, it becomes editable.

Ashwin G
  • 1,570
  • 1
  • 16
  • 23