Loading a WebView with a specific offset - webview

i'm building an Application that should load an html rendered webpage, it works but, as should be, it loads the view from the top of the page, what if i want it to load from a specific offset ? i've tryed to add the parameters ScrollX and ScrollY in the layout, but it just made the app crash... so how can i achieve such a behavior?
thanks in advance :)

You can set the contentOffset of your WEbView-
And the code will be like this -
[webView setContentOffset:CGPointMake(0,-20) animated:YES];

Related

It seems to me that my UIWebView is behind other views

I am adding a new uiwebview in order to login.
One day(After updating to iOS9, but I think that this is not the reason) I failed to show this uiwebview.
So, what can I do to be sure, that my view is at the top ?
Is there any way to programmatically get it higher ?
(void)bringSubviewToFront:(UIView * _Nonnull)view
Use this on the superview of your UIWebView:
eg:
[self.view bringSubviewToFront:self.webView];

Force to not reload a uiwebview

I'm working in a news feed.
I have a uiwebview who load an url and show all the news. The problem is, that page has a button: "Load more news" at the bottom of the web page. So, when the user press there, the page load more news.
The problem happen, when we have a video in one of our news. Because, when the user press over "load more news" the uiwebview refresh the page and the scroll go back to the top of position.
So... my question is: There are a way to solve this?
Can we stop a refresh?
Thanks.
Regards
You could save the position of the webview before the load more news button is tapped in a variable
CGPoint scrollOffset = self.webView.scrollView.contentOffset;
And you can later restore its position by setting the same scrollOffset to the webview again:
[self.webView.scrollView setContentOffset:scrollOffset];
You can try to handle UIWebView delegate -shouldStartRequest and return NO in your case.

How to programmatically simulate a double-tap on a UIWebView?

My app has a UIWebView that displays HTML emails. If I zoom in on the content and then double-tap on it a couple of times, the email content eventually gets resized so that it all fits on the screen again.
I'd like to programmatically reproduce this resizing behavior (I want to be able to call a function that re-scales the content so that it all fits on the screen); calling sizeToFit on the web view does not seem to have any effect.
If you know the CGRect you want to scale it to, then you can use,
- (void)zoomToRect:(CGRect)rect animated:(BOOL)animated
As UIWebView is scrollable, So I guess you should be able to do this.
Apparently your UIWebView should have an iVar named scrollView. So, if your UIWebView object name is webView. just do something like, below code might not be 100% syntactically correct. But you must have got the idea.
[webView.scrollView zoomToRect:CGMakeRect(0,0,0,0) animated:YES];

Trouble figuring out how to add a custom view above WebView within the same ScrollView

I'm trying to create an app that will have an interface similar to the Mail app on the iPhone. When you view an email message on the built-in Mail app, it displays sender/receiver information in addition to the subject in an area before the message. I was thinking of using a WebView for the message portion, but I can't figure out a way to have some custom view appear above the WebView while still within the same ScrollView.
What is the best way to implement this? To me the best option seems to be generating the "custom view" at the top in HTML/CSS, and simply appending the message's HTML afterward. I've looked at trying to put a WebView with another view inside a ScrollView, but it seems to go against Apple's design guidelines.
Any ideas or suggestions would be great.
Thanks,
Thomas
I don't hink it is against Apple guidelines.
What I'd do in your case is the following.
First create the scroll view
Place inside it your fixed height header (with subject, etc...)
Then place inside the scrollview a webView with an y offset of your headers height.
After the webView is done loading call [webView sizeToFit]; (wich resizes the view trying not to crop subViews)
Maybe you'll have to set the y offset again (frame.origin.y=offset)
And then set the scrollview content size to the sum of the two heights:
scrollView.contentSize=CGSizeMake(width, headerView.frame.size.height + webview.frame.size.height);
I'm not really confident with what [webView sizeToFit] is going to do. I've read you can also use [webView sizeThatFits:CGSizeZero]; . It will apparently return the needed size for the webView.
The you would simply have to change the webviews frame size to the return value of that function and use the same contentsize statement as before.
Hope this helped
PS: I think it's a bad idea to use html as a replacement for views. It's much slower and it takes initial time to load. The only use I've found to it is for displaying formatted text.

Clear UIWebView content upon dismissal of modal view (iPhone OS 3.0)

I currently have a UIWebView that is displayed within a modal view. It is basically a detail view that provides a view of a page when the user clicks a link. When the view is dismissed and then brought up again (when the user clicks another link), the previously-loaded content is still visible and the new content loads "on top" of the last content. This makes sense because the instance of the UIWebView persists between sessions and is only released when the memory is needed.
However, I would like to completely clear the UIWebView when the modal view is dismissed so that 1) content is cleared and 2) memory is freed. Thus far my research and attempts have not found an answer. These links haven't worked for me:
is it possible to free memory of UIWebView?
Reused UIWebView showing previous loaded content for a brief second on iPhone
I've tried [[NSURLCache sharedURLCache] removeAllCachedResponses]; and setting the webView to nil and manually releasing the webView upon modal-view-dismiss to no avail. Any thoughts from the wizened masses?
Sometimes loadHTMLString is too slow. You also can use:
[webView stringByEvaluatingJavaScriptFromString:#"document.body.innerHTML = \"\";"];
I'm not sure if that will free up much memory though...
Releasing the web view is probably the best approach. If a view is in a view hierarchy (has a superview) you must call removeFromSuperview to get the superview to release the view.
You could also load an html string for an empty document:
[webView loadHTMLString:#"<html><head></head><body></body></html>" baseURL:nil];
It seems under some circumstances the methods suggested by drawnonward and rob may not work (possibly something to do with CSS on the page that's being cleared), however this did work :
[webView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:#"about:blank"]]];
evaluating javascript is usually quicker than loading a new request.
[webview stringByEvaluatingJavaScriptFromString:#"document.open();document.close();"];
In my case I needed to clear the web view from its contents instantaneously and be able to start loading something else right away. Neither loading an empty document HTML string nor clearing the page with JavaScript resulted in desired effect. Instead I used:
[webView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:#"about:blank"]]];
[webView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:#"http://stackoverflow.com"]]];
And this cleared the view to the default background and initiated next page's loading as expected.
I hope this helps someone else as well.
i prefer to the following:
[m_webView stringByEvaluatingJavaScriptFromString:#"location.replace('about:blank')"]

Resources