I do exactly what you are describing in my app. This is the code I use, verbatim. It's very nicely animated, and seems to work wonderfully Two disclaimers: I pass in parentView as part of a custom initialization I did not write this code, I'm not taking credit for it.
I got it from Matt Gallagher's truly wonderful blog Cocoa With Love static const CGFloat KEYBOARD_ANIMATION_DURATION = 0.3; static const CGFloat MINIMUM_SCROLL_FRACTION = 0.2; static const CGFloat MAXIMUM_SCROLL_FRACTION = 0.8; // adjust this following value to account for the height of your toolbar, too static const CGFloat PORTRAIT_KEYBOARD_HEIGHT = 216; - (void)textFieldDidEndEditing:(UITextField *)textField { CGRect viewFrame = self.parentView. Frame; viewFrame.origin. Y += animatedDistance; UIView beginAnimations:nil context:NULL; UIView setAnimationBeginsFromCurrentState:YES; UIView setAnimationDuration:KEYBOARD_ANIMATION_DURATION; self.
ParentView setFrame:viewFrame; UIView commitAnimations; } - (void) textFieldDidBeginEditing:(UITextField*) textField { CGRect textFieldRect = self.parentView. Window convertRect:textField. Bounds fromView:textField; CGRect viewRect = self.parentView.
Window convertRect:self.parentView. Bounds fromView:self. ParentView; CGFloat midline = textFieldRect.origin.
Y + 0.5 * textFieldRect.size. Height; CGFloat numerator = midline - viewRect.origin. Y - MINIMUM_SCROLL_FRACTION * viewRect.size.
Height; CGFloat denominator = (MAXIMUM_SCROLL_FRACTION - MINIMUM_SCROLL_FRACTION) * viewRect.size. Height; CGFloat heightFraction = numerator / denominator; if (heightFraction 1.0) { heightFraction = 1.0; } animatedDistance = floor(PORTRAIT_KEYBOARD_HEIGHT * heightFraction); CGRect viewFrame = self.parentView. Frame; viewFrame.origin.
Y -= animatedDistance; UIView beginAnimations:nil context:NULL; UIView setAnimationBeginsFromCurrentState:YES; UIView setAnimationDuration:KEYBOARD_ANIMATION_DURATION; self. ParentView setFrame:viewFrame; UIView commitAnimations; }.
I do exactly what you are describing in my app. This is the code I use, verbatim. It's very nicely animated, and seems to work wonderfully.
Two disclaimers: I pass in parentView as part of a custom initialization. I did not write this code, I'm not taking credit for it. I got it from Matt Gallagher's truly wonderful blog Cocoa With Love.
Static const CGFloat KEYBOARD_ANIMATION_DURATION = 0.3; static const CGFloat MINIMUM_SCROLL_FRACTION = 0.2; static const CGFloat MAXIMUM_SCROLL_FRACTION = 0.8; // adjust this following value to account for the height of your toolbar, too static const CGFloat PORTRAIT_KEYBOARD_HEIGHT = 216; - (void)textFieldDidEndEditing:(UITextField *)textField { CGRect viewFrame = self.parentView. Frame; viewFrame.origin. Y += animatedDistance; UIView beginAnimations:nil context:NULL; UIView setAnimationBeginsFromCurrentState:YES; UIView setAnimationDuration:KEYBOARD_ANIMATION_DURATION; self.
ParentView setFrame:viewFrame; UIView commitAnimations; } - (void) textFieldDidBeginEditing:(UITextField*) textField { CGRect textFieldRect = self.parentView. Window convertRect:textField. Bounds fromView:textField; CGRect viewRect = self.parentView.
Window convertRect:self.parentView. Bounds fromView:self. ParentView; CGFloat midline = textFieldRect.origin.
Y + 0.5 * textFieldRect.size. Height; CGFloat numerator = midline - viewRect.origin. Y - MINIMUM_SCROLL_FRACTION * viewRect.size.
Height; CGFloat denominator = (MAXIMUM_SCROLL_FRACTION - MINIMUM_SCROLL_FRACTION) * viewRect.size. Height; CGFloat heightFraction = numerator / denominator; if (heightFraction 1.0) { heightFraction = 1.0; } animatedDistance = floor(PORTRAIT_KEYBOARD_HEIGHT * heightFraction); CGRect viewFrame = self.parentView. Frame; viewFrame.origin.
Y -= animatedDistance; UIView beginAnimations:nil context:NULL; UIView setAnimationBeginsFromCurrentState:YES; UIView setAnimationDuration:KEYBOARD_ANIMATION_DURATION; self. ParentView setFrame:viewFrame; UIView commitAnimations; }.
I'm assuming you're referring to this blog post at cocoawithlove. Com/2008/10/… - I've seen it too, and it doesn't really deal with UITableViews, only static views with UITextFields. The issue with this approach is that the animatedDistance may change as the user scrolls through the table view.
– Tim Jul 20 '09 at 23:28 I'm using it inside a UITableView with no issue whatsoever. – mmc Jul 20 '09 at 23:57 I implemented this, but on closer inspection of the code and results all it does is move the origin of the table view up off the screen. It has the effect described in the blog post, but makes it impossible for the user to see the top few table view cells when editing a cell near the bottom of the table.
– Tim Jul 21 '09 at 1:30 I guess I don't understand the question then. There is only so much screen space. I'm not sure how you can show the cells at the top, edit the cells at the bottom, and show the keyboard.
– mmc Jul 21 '09 at 2:33 Maybe I'm implementing something slightly off - what view do you pass in for parentView in your initializer? I'm using self. TableView in lieu of parentView, but that might be messing with it.
– Tim Jul 21 '09 at 21:52.
I gave that a try - the table view seems to resize OK, then chokes pretty badly if you try to scroll a cell that was in the area that got resized away back onto the screen. – Tim Jul 12 '09 at 20:47.
Since all UITableViews are also UIScrollViews, you can call all of the standard scroll methods on them. Something like this will do: - (void)scrollTableView:(UITableView *)tableView toIndexPath:(NSIndexPath *)indexPath withBottomPadding:(CGFloat)bottomPadding { CGRect rectForRow = tableView rectForRowAtIndexPath:indexPath; CGRect bounds = tableView bounds; CGPoint contentOffset = tableView contentSize; if (rectForRow.origin. Y + rectForRow.size.
Height > contentOffset. Y + bounds.size. Height - bottomPadding) tableView setContentOffset:rectForRow.origin.
Y + rectForRow.size. Height - bounds.size. Height - bottomPadding animated:YES; else tableView scrollToRowAtIndexPath:indexPath atScrollPosition:UITableViewScrollPositionNone animated:YES; } Note: I'm not at a Mac with Xcode at the moment.
I will test this code when I am.
I've been asking a question or two over the past few days of working on an application that keeps a custom toolbar aligned to the top of the iPhone keyboard. I'm using the method described by Josh in this question; basically, I have the view controller listen for the UIKeyboardWillShowNotification and add the toolbar as necessary. The view controller itself manages a table view whose cells all contain a UITextField.
The keyboard and toolbar being presented are editing these text fields. The only issue I'm still having is this: when the keyboard and toolbar are presented for a cell more than about halfway down the table, it scrolls to be visible above the keyboard, but not above the toolbar. The cells and text fields are still editable, but about half the cell is hidden under the toolbar.
If I disappear the toolbar (don't add it in the notification responder), the entire cell becomes visible, but obviously I lose the functionality the toolbar provides. Is there a way to change the location the text field gets scrolled to? I've tried playing around with the UITableView method scrollToRowAtIndexPath:atScrollPosition:animated:, but it tends to give weird results when toggling through several cells.
What's the best method for scrolling a table view cell to a visible position above a custom keyboard toolbar?
I do exactly what you are describing in my app. This is the code I use, verbatim. It's very nicely animated, and seems to work wonderfully.
Assuming that you know the NSIndexPath that you would show above the keyboard, you can use the following fast and clean method.
I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.