How to add subview to a webview so that the subview would scroll along with webview?
Asked Answered
M

4

8

I have webview.

Now I want to add a subview to it. I added it using addsubview method.

The view got added, but did not scrolled with webview.

I want my subview to be scrolled with the webview.

How can I do that?

Regards, Akshay.

Moonshine answered 27/7, 2010 at 8:28 Comment(2)
That's strange to add a subview to an webview. What are you trying to do ?Bradlybradman
Here is the solution https://mcmap.net/q/1324281/-uiview-scroll-with-webview/6521116Cacilia
T
17

You simply need to add your subview to the UIScrollView of the UIWebView like this to have your subview scroll with the webview content:

[webView.scrollView addSubview:youSubView];
Telex answered 13/2, 2012 at 10:31 Comment(3)
For the record, the scrollView property was added in iOS 5.0.Think
indeed only available post-iOS 5.0, but this is a fabulous trick; i just used it to allow my spinner for loading into a webView to be housed withing the confines of the UIWebView. perfect!Capeskin
it can only help me to add upto 126 subview'sSev
L
0

In order to do that, you would need to tinker with the internals of UIWebView and find out it's view hierarchy, so you can add the subview inside the scoller.

It's probably easier to tweak your HTML inside the view, so you can include something similar to your custom view (text or an image) in it.

You can later interact with your "fake view" via Javascript.

Lightproof answered 27/7, 2010 at 14:55 Comment(1)
Is it possible if I have a UIWebView object? I have to work with existing webView object and cannot create new one by subclassing..Moonshine
H
0

Try this, it will work

[[[yourwebview subviews] objectAtIndex:0] addSubview:yoursubview];

Hectic answered 24/9, 2014 at 9:28 Comment(0)
C
0

Suppose you want your uiview on a region with movie id in the html. Check my post here.

func scrollViewDidScroll(_ scrollView: UIScrollView) {
    self.wk.evaluateJavaScript("var rect = document.getElementById('movie').getBoundingClientRect();[rect.left, rect.top];") {
        (result, error) -> Void in
        print(result)
        self.player.view.frame.origin.x = (result as! Array)[0]
        self.player.view.frame.origin.y = (result as! Array)[1]
    }
}
Cacilia answered 8/5, 2017 at 5:35 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.