Reputation: 423
I've done large enough research but couldn't find answer to my question.
Suppose I have a webView
in which I have some text fields, some of them are placed on the bottom of screen so that when keyboard appears it should hide that fields. After keyboard appears the content of webView
slides up in order to make that fields visible. The problem is that I DON'T want the content to slide up.
How can I disable that feature of webview
, or somehow make the content not scroll up?
Upvotes: 11
Views: 10725
Reputation: 1
Swift5
zoom and scroll hold
class WebviewScrollViewDelegate: NSObject, UIScrollViewDelegate {
var webview: WKWebView?
func scrollViewWillBeginZooming(_ scrollView: UIScrollView, with view: UIView?) {
scrollView.pinchGestureRecognizer?.isEnabled = false
}
func scrollViewDidScroll(_ scrollView: UIScrollView) {
if let webview = webview {
scrollView.bounds = webview.bounds
scrollView.zoomScale = webview.pageZoom
}
}
}
class MyWebview {
let webviewScrollViewDelegate = WebviewScrollViewDelegate()
webview.scrollView.delegate = webviewScrollViewDelegate
webviewScrollViewDelegate.webview = webview
}
Upvotes: 0
Reputation: 335
Originally I was looking to stop my webview from zooming in when an input field (in my case the comments section) was focused on. The entire view would zoom in automatically and put everything out of whack. I managed to do it with this, and at the same time I got the webview to stop scrolling up and out of the way of the soft keyboard as well:
//Stop the webview from zooming in when the comments section is used.
UIScrollView *scrollView = [_webView.subviews objectAtIndex:0];
scrollView.delegate = self;
//_webView.scrollView.delegate = self; //for versions newer than iOS5.
Upvotes: 0
Reputation: 3216
If you want to disable ALL scrolling, including the auto-scroll when you navigate between form fields, setting webView.scrollView.scrollEnabled=NO
doesn't quite cover everything. That stops normal tap-and-drag scrolling, but not the automatic bring-field-into-view scrolling when you navigate around a web form.
Also, watching for UIKeyboardWillShowNotification
will let you prevent scrolling when the keyboard appears, but that will do nothing if the keyboard is already up from editing a different form field.
Here's how to prevent ALL scrolling in three simple steps:
1) After you create the UIWebView, disable normal scrolling:
myWebView.scrollView.scrollEnabled = NO;
2) Then register your view controller as the scrollView's delegate:
myWebView.scrollView.delegate = self;
(And make sure to add <UIScrollViewDelegate>
to your class's @interface
definition to prevent compiler warnings)
3) Capture and undo all scroll events:
// UIScrollViewDelegate method
- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
scrollView.bounds = myWebView.bounds;
}
Upvotes: 20
Reputation: 423
I found the solution for me. I just scroll it down again after scrolling up.
First I catch notification UIKeyboardWillShowNotification
by adding observer to it
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillShow:) name:UIKeyboardWillShowNotification object:nil];
then implement method:
-(void)keyboardWillShow:(NSNotification*)aNotification{
NSDictionary* info = [aNotification userInfo];
float kbHeight = [[NSNumber numberWithFloat:[[info objectForKey:UIKeyboardFrameBeginUserInfoKey] CGRectValue].size.height]floatValue];
float kbWidth = [[NSNumber numberWithFloat:[[info objectForKey:UIKeyboardFrameBeginUserInfoKey] CGRectValue].size.width]floatValue];
BOOL keyboardIsVisible=[self UIKeyboardIsVisible];
//Check orientation and scroll down webview content by keyboard size
if(kbWidth==[UIScreen mainScreen].bounds.size.width){
if (!keyboardIsVisible) {
[[chatView scrollView] setContentOffset:CGPointMake(0, -kbHeight+10) animated:YES];
} //If is landscape content scroll up is about 113 px so need to scroll down by 113
}else if (kbHeight==[UIScreen mainScreen].bounds.size.height){
if (!keyboardIsVisible) {
[[chatView scrollView] setContentOffset:CGPointMake(0, -113) animated:YES];
}
}
}
This is not what exactly I asked but helped me to solve my problem.
Thanks.
Upvotes: 0