nonuma nonuma - 26 days ago 11
iOS Question

UITextField pops up too high when using TPKeyboardAvoiding class

I'm using the

class to make sure the keyboard never pops over the
UITextField
. However there is some strange bug on the iPhone, when I click on the
UITextField
the textfield goes up but too high:

enter image description here

On the GitHub link the Notes say this:


Notes

These classes currently adjust the contentInset parameter to
avoid content moving beneath the keyboard. This is done, as opposed to
adjusting the frame, in order to work around an iOS bug that results
in a jerky animation where the view jumps upwards, before settling
down. In order to facilitate this workaround, the contentSize is
maintained to be at least same size as the view's frame.


Maybe it has something to do with this, I'm just not sure how I can fix this problem.

I already tried changing this line:

[self setContentOffset:CGPointMake(self.contentOffset.x,
[self idealOffsetForView:firstResponder withSpace:[self keyboardRect].origin.y - self.bounds.origin.y])


with (added +100):

[self setContentOffset:CGPointMake(self.contentOffset.x,
[self idealOffsetForView:firstResponder withSpace:[self keyboardRect].origin.y - self.bounds.origin.y+100])


but this isn't a good way to do it since this won't work on the iPad.

Answer

Had the same problem recently. For me, solution was to modify -(CGFloat)idealOffsetForView:(UIView *)view withSpace:(CGFloat)space method. Here is how it looks in my project:

-(CGFloat)idealOffsetForView:(UIView *)view withSpace:(CGFloat)space {

// Convert the rect to get the view's distance from the top of the scrollView.
CGRect rect = [view convertRect:view.bounds toView:self];

// Set starting offset to that point
CGFloat offset = rect.origin.y;


if ( view.bounds.size.height < space ) {
    // Center vertically if there's room
    offset -= floor((space-view.bounds.size.height)/2.0);
}
if ( offset + space > self.contentSize.height ) {
    // Clamp to content size
    offset = self.contentSize.height - space;
}

if (offset < 0) offset = 0;

return offset;
}

The change was to remove first path from conditional block: if ( self.contentSize.height - offset < space ) and leave just "else" path.