Disable scrolling in UIWebView
webView.userInteractionEnabled = NO;
Please note that this technic disables touches and interaction on the web view
how to disable all user interaction in UIWebView except scroll interaction
You should use this delegate function:
- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType;
And return NO.
ios - Enable scroll and zoom in UIWebView but disable other user interaction
The Psiticosis answer is the easier approach to accomplish this, but it's easier to add the "transparent view" into the document body.
Just execute a script on your WebView that add an overlay on the document's body.
The script can be like this:
//CSS setup
var styletag = document.createElement("style");
styletag.type = "text/css";
styletag.innerHTML = '#naughty-overlay {position:fixed; height:100%; z-index:9999;}';
styletag.innerHTML += '* {-webkit-user-select:none;}';
//Create the overlay div
var overlay = document.createElement('div');
overlay.id = 'naughty-overlay';
//Append elements
document.body.appendChild(styletag);
document.body.appendChild(overlay);
How do I enable horizontal scrolling and disable vertical scrolling in UIWebview?
Enable Horizontal scrolling and disable Vertical scrolling:
myWebView.scrollView.delegate = self;
[myWebView.scrollView setShowsVerticalScrollIndicator:NO];
-(void)scrollViewDidScroll:(UIScrollView *)scrollView
{
if (scrollView.contentOffset.y > 0 || scrollView.contentOffset.y < 0 )
scrollView.contentOffset = CGPointMake(scrollView.contentOffset.x, 0);
}
the 2nd line there will hide the vertical scroll indicator. Checking if "scrollView.contentOffset.y < zero" will disable the bouncing when you attempt to scroll downwards. You can also do: scrollView.bounces=NO to do the same thing!!
By just looking at Rama Rao's answer i can tell that his code will reset the scrollView to (0.0) the moment you try to scroll vertically, thereby moving you away from your horizontal position, which is not good.
Enable vertical scrolling and disable Horizontal scrolling:
myWebView.scrollView.delegate = self;
[myWebview.scrollView setShowsHorizontalScrollIndicator:NO];
-(void)scrollViewDidScroll:(UIScrollView *)scrollView
{
if (scrollView.contentOffset.x > 0)
scrollView.contentOffset = CGPointMake(0, scrollView.contentOffset.y);
}
PEACE
Why Shouldn't A UIWebView Be Placed in a UIScrollView?
The only reason Apple does not recommend putting UIWebViews
inside UIScrollViews
if for the one you explain: because scrolling would risk to be mixed up between the two scroll views.
I guess they wrote this because, by the fact that UIWebView
inherits UIView
and not UIScrollView
, and thus is not a scrollview itself (but embeds one), this may be not obvious for the unexperimented user that the web content can be scrollable depending on the HTML, which would mess up with the container scrollview if any. So that is probably just a reminder for this case.
But if you disable the scrolling, I can't see any reason why this would go wrong.
Note anyway that disabling user interaction on the scrollview is not the same as disabling scrolling. If your HTML content contains links or other clickable/tappable content, disabling user interaction will disable them too. To only disable scrolling only but keep user interaction like simple taps, use webview.scrollView.scrollEnabled = NO
instead of webview.scrollView.userInteractionEnabled = NO
.
Related Topics
How to Force My Keyboard to Be Up on My Program's Start in Swift
How to Parse This JSON in Swift
How to Change the Image Displayed in a Uiimageview Programmatically
Uicollectionview's Cellforitematindexpath Is Not Being Called
Get Latitude/Longitude from Address
How to Add Headerview in Uicollectionview Like Uitableview's Tableheaderview
Icon Already Includes Gloss Effects
How to Add Textfield to Uialertcontroller in Swift
Uilabel Text Not Being Updated
How to Return Value After the Execution of the Block? Swift
Waiting for Asynchronous Function Call to Complete
Uitableviewcell Separator Disappearing in iOS7
When to Use Uicollectionview Instead of Uitableview
Module Was Not Compiled for Testing' When Using @Testable
Any Way to Print in Color with Nslog
How to Open Location Services Screen from Setting Screen
Disable the Uitableview Highlighting But Allow the Selection of Individual Cells