how to forward UIWebView scroll to UIScrollView ?

2019-04-07 08:33发布

I have a UIWebView inside UIScrollView.

This idea is to be able to create more reading space on screen when user scroll the webpage upwards - by scrolling the UIScrollView upwards till the toolbar is visible, and obviously when the toolbars is nomore visible actually scroll the webpage to show more content that's on the page.

IPhone Safari browser does exactly the same functionality.

see screenshot (first) for default behavior i am getting - i guess because : the scrolling message is consumed by the webview since the touch/scroll is happening directly on webview area.

what i would like to do here is programatiicaly decide when to forward the 'upward scroll' to the UIScrollivew and when not to.

Any tips on how to get around this will be so helpful. Thanks!!

enter image description here

enter image description here

3条回答
仙女界的扛把子
2楼-- · 2019-04-07 08:35

The UIWebView class reference discourages embedding a UIWebView in a UIScrollView:

Important: You should not embed UIWebView or UITableView objects in UIScrollView objects. If you do so, unexpected behavior can result because touch events for the two objects can be mixed up and wrongly handled.

However I suppose you still want to implement your feature ;-)

Idea 1: Don't embed the UIWebView in a UIScrollView, instead run javascript using UIWebView's stringByEvaluatingJavaScriptFromString: method, modifying the DOM to add a toolbar below. If required, you can callback objective-c code from any buttons pushed on the toolbar by registering some custom URL schemes.

Idea 2: Don't embed the UIWebView in a UIScrollView, but in a normal UIView. Building on Vignesh's suggestion, listen for your webView's inner scrollView's scrollViewDidScroll: callback via the delegate, checking the contentOffset vs. the contentSize's height each time the callback is called. Once they are equal, it means you got to the bottom. Once this happens you can animate your toolbar's frame to "enter" the containing UIView and "push" the webView's frame away.

Idea 3: Ignore Apple's recommendation, and embed the UIWebView in a UIScrollView. Building on Vignesh's suggestion, listen for your webView's inner scrollView's scrollViewDidScroll: callback via the delegate, checking the contentOffset vs. the contentSize's height each time the callback is called. Once they are equal, it means you got to the bottom. Once this happens set the userInteractionEnabled property of the webView to NO, and set it to YES on the scrollView which contains the webView and the toolbar. Hopefully the scroll will continue smoothly enough. Of course you have to listen to the containing scroll view in the same way to determine when to switch back the userInteractionEnabled.

A variation on this idea would be to just set userInteractionEnabled to NO for the webView, but set the webView's frame's height to match its contentSize, and also enlarge the contentSize of the containing scrollView accordingly.

Both variations have the drawback that in some cases you won't be able to do things such as click on links :-( But maybe that's good enough for your case. At least in the first variation it's not so bad.

查看更多
唯我独甜
3楼-- · 2019-04-07 08:55
何必那么认真
4楼-- · 2019-04-07 08:56

You can add a searchbox and uiwebview in a UIscrollview one below another. To get the content offset when webview is scrolled you can use the following code snippet.

UIScrollView* currentScrollView;
for (UIView* subView in testWebView.subviews) {
    if ([subView isKindOfClass:[UIScrollView class]]) {
        currentScrollView = (UIScrollView*)subView;
        currentScrollView.delegate = (id)self;
    }
}

Now you can change your base scrollview offset's y value to the offset value you get from the scrollview didscroll delegate method.

查看更多
登录 后发表回答