Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

slammers

macrumors newbie
Original poster
Sep 28, 2008
6
0
I need to scroll my table view when the keyboard is displayed. I know how to scroll the screen but I can not figure out how to determine how much scrolling is required.

My code determines which cell in a table view is being edited. I would like to figure out the location of that cell on the screen so I can determine if scrolling is required to prevent it from being hidden by the keyboard.

The contentView of the cell always has 0,0 origin. Is there any way to determine to actual location of the cell on the screen or determine the cells offset from the top of the view its parent view?

Is there an easier way to have the cell automatically moved so the keyboard does not hide it? I thought that would be automatic but in the UICatalog example they are programmatically scrolling the screen.

Thanks
Shawn
 

beachdog

macrumors member
Aug 10, 2008
86
0
I did it like this:

Code:
-(void)textFieldDidBeginEditing:(UITextField *)textField {
	if( textField == txtCarbs ) {
		CGRect rc = [textField bounds];
		UIScrollView* v = (UIScrollView*) self.view ;
		rc = [textField convertRect:rc toView:v];
		CGPoint pt = rc.origin ;
		pt.x = 0 ;
		[v setContentOffset:pt animated:YES];
	}
}

This code scrolls my view when the user enters a certain text field such that the text field appears at the top of the view.

I am wondering, though, whether there is a "best practices" for how much to scroll the view. For example, the code above shows the current field at the top, but maybe it would be better to show the control above that one at the top, so that anytime you select a field you can see the one directly above it and below it, so you can easily navigate either way, up or down?

While that code worked for me in a standard UIScrollView, I was not sure how to have scrollbars appears so that the user could also manually scroll the view if he/she wanted to. I kind of thought a UIScrollView would take care of that automatically, if my content view was bigger than the scrollview itself, but it did not. Is there a trick I am missing?
 

TonyHoyle

macrumors 6502a
Sep 14, 2007
999
0
Manchester, UK
I do something like this between each editable field:

Code:
- (IBAction)usernameExit:(id)sender
{
        [password becomeFirstResponder];
}

That causes a scroll, although the effect isn't perfect (it scrolls until the item is visible but often stops when it's *just* visible so you can get half a field).

You should be able to get the dimensions of the editable field and code a better scroll.. ideally just scroll the minimum required to put it in view fully. I'll probably do something like that on my next project.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.