Scroll Uitextfield Above Keyboard in a Uitableviewcell on a Regular Uiviewcontroller

Scroll UITextField above Keyboard in a UITableViewCell on a regular UIViewController

I spent all day trying to figure this out. I posted it here, then found a blog link and an incredibly simple solution. It looks like this:

-(BOOL)textFieldShouldBeginEditing:(UITextField *)textField {
CGPoint pointInTable = [textField.superview convertPoint:textField.frame.origin toView:self.tableView];
CGPoint contentOffset = self.tableView.contentOffset;

contentOffset.y = (pointInTable.y - textField.inputAccessoryView.frame.size.height);

NSLog(@"contentOffset is: %@", NSStringFromCGPoint(contentOffset));

[self.tableView setContentOffset:contentOffset animated:YES];

return YES;
}


-(BOOL)textFieldShouldEndEditing:(UITextField *)textField
{
[textField resignFirstResponder];

if ([textField.superview.superview isKindOfClass:[UITableViewCell class]])
{
UITableViewCell *cell = (UITableViewCell*)textField.superview.superview;
NSIndexPath *indexPath = [self.tableView indexPathForCell:cell];

[self.tableView scrollToRowAtIndexPath:indexPath atScrollPosition:UITableViewScrollPositionMiddle animated:TRUE];
}

return YES;
}

Check this for iOS 8

Get UITableView to scroll to the selected UITextField and Avoid Being Hidden by Keyboard

In my app, I have successfully used a combination of contentInset and scrollToRowAtIndexPath like this:

When you want to display the keyboard, just add a contentInset at the bottom with your table with desired height:

tableView.contentInset =  UIEdgeInsetsMake(0, 0, height, 0);

Then, you can safely use

[tableView scrollToRowAtIndexPath:[NSIndexPath indexPathForRow:cell_index inSection:cell_section] animated:YES];

By adding the contentInset, even if you are focusing on the last cell the tableView will still be able to scroll. Just make sure that when you are dismissing the keyboard, you reset the contentInset.

EDIT:

If you have only one section (you can replace cell_section with 0) and the use the textView tag to inform the cell row.

How to scroll UITextView above Keyboard in a UITableView in swift

This is one way to avoid the error in your code:

func textViewShouldBeginEditing(textView: UITextView) -> Bool {
var pointInTable:CGPoint = textView.superview!.convertPoint(textView.frame.origin, toView: tableView)
var contentOffset:CGPoint = tableView.contentOffset
contentOffset.y = pointInTable.y
if let accessoryView = textView.inputAccessoryView {
contentOffset.y -= accessoryView.frame.size.height
}
tableView.contentOffset = contentOffset
return true
}

First of all, we can assume textView has a superview, so we can unwrap it with !.

As a result, pointInTable is not optional anymore.

If we assume textView might not have an inputAccessoryView, we can use the typical if let syntax to check if inputAccessoryView exists and, if so, subtract its height from contentOffset.y.

Don't forget to assign contentOffset to your tableView and return a Bool.

Scroll UITextField above Keyboard in a UITableViewCell on a regular UIViewController

I spent all day trying to figure this out. I posted it here, then found a blog link and an incredibly simple solution. It looks like this:

-(BOOL)textFieldShouldBeginEditing:(UITextField *)textField {
CGPoint pointInTable = [textField.superview convertPoint:textField.frame.origin toView:self.tableView];
CGPoint contentOffset = self.tableView.contentOffset;

contentOffset.y = (pointInTable.y - textField.inputAccessoryView.frame.size.height);

NSLog(@"contentOffset is: %@", NSStringFromCGPoint(contentOffset));

[self.tableView setContentOffset:contentOffset animated:YES];

return YES;
}


-(BOOL)textFieldShouldEndEditing:(UITextField *)textField
{
[textField resignFirstResponder];

if ([textField.superview.superview isKindOfClass:[UITableViewCell class]])
{
UITableViewCell *cell = (UITableViewCell*)textField.superview.superview;
NSIndexPath *indexPath = [self.tableView indexPathForCell:cell];

[self.tableView scrollToRowAtIndexPath:indexPath atScrollPosition:UITableViewScrollPositionMiddle animated:TRUE];
}

return YES;
}

Check this for iOS 8

Tableview scroll content when keyboard shows

Try keeping the editing index path editingIndexPath Getting index path and scroll tableview to that index path

func keyboardWasShown (notification: NSNotification)
{
println("keyboard was shown")
var info = notification.userInfo
var keyboardSize = info.objectForKey(UIKeyboardFrameBeginUserInfoKey).CGRectValue().size

var contentInsets:UIEdgeInsets

if UIInterfaceOrientationIsPortrait(UIApplication.sharedApplication().statusBarOrientation) {

contentInsets = UIEdgeInsetsMake(0.0, 0.0, keyboardSize.height, 0.0);
}
else {
contentInsets = UIEdgeInsetsMake(0.0, 0.0, keyboardSize.width, 0.0);

}

myTableView.contentInset = contentInsets

myTableView.scrollToRowAtIndexPath(editingIndexPath, atScrollPosition: .Top, animated: true)
myTableView.scrollIndicatorInsets = myTableView.contentInset
}

Scroll to UITableViewCell that contains a certain UITextField

I do this all the time. The way that I do it is I have a method that is called in the UIControlEventEditingDidBegin for the textfield, and in that method, I do:

-(void)startEdit:(UITextField *)textField {
self.prevOffset = self.tableView.contentOffset.y; //I like storing the current offset so I can restore it when the text stops editing, you don't have to do this.
int offSet = [textField superview].frame.origin.y; //this gets the y coordinate of the cell the textField is in. If the table is not at 0,0, you also need to add [[textField superview] superview].frame.origin.y;
offSet-=(self.view.frame.size.height-KEYBOARD_HEIGHT)/2; //where KEYBOARD_HEIGHT is 216 in portrait and 160 in landscape;
if (offSet<0) offSet = 0;
[UIView animateWithDuration:0.3 animations:^{
[self.tableView setContentOffset:CGPointMake(0,offSet)];}];
}

I do a lot of other things as well, but I believe they are specific for my application.

First, if the offset is greater than 0, I set teh contentInset to UIEdgeInsetsMake(0,0,KEYBOARD_HEIGHT,0) because I was having some jumpy scrollViews before I did that.

Also, if the original offset (self.prevOffset) plus the frame's height is greater than the content size (which would also cause jumping as it sets the offset too low then jumps back), I set the prevOffset to MAX(0,contentSize.height-frame.size.height).

These things aren't neccessary, but you are getting Scroll/TableViews that are jumping around, try them out.

Why won't UITableView 'Auto-scroll' when editing UITextField (in UITableViewCell)? (iPhone)

Check out TaggedLocations sample code from apple. It does the same thing without any extra manipulation.

The key is that your viewcontrollers are following the standard. i.e you are NOT having container viewcontrollers such as UINavigationController within UIViewController.



Related Topics



Leave a reply



Submit