Reputation: 2638
I have a UIWebView
with login fields that require user input for an iPad app. The view is presented modally and when landscape the keyboard is presented which causes the UIWebView
to scroll up.
Here's where the bug happens - the UIWebView
contains a tap delay which I haven't found a reliable solution around it. (I'm using OAuth
so any Javascript
injecting hasn't worked reliably). The keyboard is presented so when I tap on the input field, the tap is registered after the view is scrolled automatically and is not in the correct spot.
Essentially, this is making it so I tap on the top input field, and the tap is registered about 20px lower than it should because the view is being shifted by the keyboard.
I've tried preventing the UIWebView
from scrolling, but the keyboard always makes it shift no matter what.
I've tried injecting Javascript
to remove the tap delay, but haven't had success there either.
Any help is appreciated!
Upvotes: 3
Views: 2644
Reputation: 21
I'm grappling with this same issue. I have not solved the tap offset issue yet but as far as turning off the tap delay you can do it using:
[[self.webView scrollView] setDelaysContentTouches:NO];
Not sure if you have found this page yet, but you can add listeners for keyboard notifications and manipulate scrolling yourself. Here is Apple's documentation link: https://developer.apple.com/library/ios/documentation/StringsTextFonts/Conceptual/TextAndWebiPhoneOS/KeyboardManagement/KeyboardManagement.html
Here is the code from the link pertaining to manipulating the view:
// Call this method somewhere in your view controller setup code.
- (void)registerForKeyboardNotifications
{
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(keyboardWasShown:)
name:UIKeyboardDidShowNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(keyboardWillBeHidden:)
name:UIKeyboardWillHideNotification object:nil];
}
// Called when the UIKeyboardDidShowNotification is sent.
- (void)keyboardWasShown:(NSNotification*)aNotification
{
NSDictionary* info = [aNotification userInfo];
CGSize kbSize = [[info objectForKey:UIKeyboardFrameBeginUserInfoKey] CGRectValue].size;
UIEdgeInsets contentInsets = UIEdgeInsetsMake(0.0, 0.0, kbSize.height, 0.0);
scrollView.contentInset = contentInsets;
scrollView.scrollIndicatorInsets = contentInsets;
// If active text field is hidden by keyboard, scroll it so it's visible
// Your app might not need or want this behavior.
CGRect aRect = self.view.frame;
aRect.size.height -= kbSize.height;
if (!CGRectContainsPoint(aRect, activeField.frame.origin) ) {
[self.scrollView scrollRectToVisible:activeField.frame animated:YES];
}
}
// Called when the UIKeyboardWillHideNotification is sent
- (void)keyboardWillBeHidden:(NSNotification*)aNotification
{
UIEdgeInsets contentInsets = UIEdgeInsetsZero;
scrollView.contentInset = contentInsets;
scrollView.scrollIndicatorInsets = contentInsets;
}
It seems like some hit areas within uiwebview don't always shift up with the keyboard. One dirty hack to get around this is to place an invisible text div over the button/etc with some spaces and have it call a javascript function that tries to accomplish whatever touch event is not registering. Like so:
<div id="someBtn" onclick="tryAction();"> </div>
I hope this helps or at least points you in a useful direction.
Upvotes: 2