scrolling UIScrollView automatically when keyboard is out

Discussion in 'iOS Programming' started by beachdog, Oct 9, 2008.

  1. beachdog macrumors member

    Aug 10, 2008
    I have a UIScrollView that has several text fields in it, and when the user touches some of the them the keyboard comes out and obscures the text field. So I have implemented code in the UITextFieldDelegate method textFieldDidBeginEditing to automatically shift the content view such that the entry field is visible above the keyboard. This works well, but now I have found that it has one bad side affect -- after repositioning the view I find that if I touch a different text field the focus does not leave the current text field that has focus. After experimenting a bit it seems that this is happening because the view system does not map my touch to the field that is now visible at the point I touched. So I assume that my implementation of shifting the content view is flawed in some regard. If someone could point out what I've done wrong, or provide an alternate implementation that would be great. Here is my code:

    -(void)textFieldDidBeginEditing:(UITextField *)textField {
    	/* scroll so that the field appears in the viewable portion of screen when the keyboard is out */
    	UIScrollView* v = (UIScrollView*) self.view ;
    	CGPoint pt ;
    	if {
    		CGRect rc = [textField bounds];
    		rc = [textField convertRect:rc toView:v];
    		CGPoint pt = rc.origin ;
    		pt.x = 0 ;
    		pt.y -= 60 ;
    		[v setContentOffset:pt animated:YES];		
  2. beachdog thread starter macrumors member

    Aug 10, 2008

    Any ideas on how to make this work?

    It seems to me that the view the keyboard is in is layered on top of the scrollview that contains the text field, and that therefore the scrollbars of the scrollview do not adjust when the keyboard comes out. I guess that means I should do that manually, but I am neither sure of the code to do that, nor how to programmatically calculate the amount of the view that the keyboard is taking.

    An example of an app that does it right is email -- when constructing an email, if you type into the To field the keyboard comes out, and then if you type a letter a tableview of matching contacts appears between the textfield and the keyboard, with proper scrolling.
  3. tusharbhatta macrumors newbie

    Jun 23, 2008
    UIScrollView firstResponder hides UITEXTFIELD


    i am newbie. Could you please help me if you have solved that problem ?

    Many thanks..

  4. mamcx macrumors regular

    Mar 13, 2008
    Hi, any update on this?

    I'm stuck on this too. I wonder what to do. No solution I get on the web solve this problem...

Share This Page