Use UIWebView with UIScrollView
When we want to display a bunch of description, UIWebView
is the choice.
Usually we do it in this way
ViewController.m
1 | _webView = [[UIWebView alloc] initWithFrame:CGRectMake(0, 0, 320, 300)]; |
What it will look like
NOTE: the “A button” is just for you to see the difference of different changes of webview
But what if we want it to transparent? Let’s try to set the background to clearColor
ViewController.m
1 | _webView.backgroundColor = [UIColor clearColor]; |
Ooopsss…. Only its background is transparent. Want to make the whole view transparent? Let’s do in this way
ViewController.m
1 | _webView.opaque = NO; |
and see the result
Hmm… it looks better. Now let’s make it smooth, make the whole thing scroll together (html content and the “A button”)
Now, the problem is we need to know how much space (the height of the webView) needed for the html content. One of the way is we can get it thru JavaScript
First, add delegate
to the webView
ViewController.h
1 | @interface ViewController : UIViewController <UIWebViewDelegate> |
ViewController.m
1 | _webView.delegate = self; |
Then implement webViewDidFinishLoad:
1 |
|
Before that, add a <div>
(with a unique ID, in this case I use “main-wrapper”) to wrap the html content, e.g.
1 | [_webView loadHTMLString:[NSString stringWithFormat:@"<div id=\"main-wrapper\">%@</div>div>", html] baseURL:nil]; |
let’s run it and see
Now it looks better, but we cannot see the content if too long, and also overlap with the button. To solve this, let’s add a UIScrollView
in between the webView
and self.view
ViewController.m
1 | _scrollView = [[UIScrollView alloc] initWithFrame:self.view.bounds]; |
Add these lines to bottom of webViewDidFinishLoad:
1 | // adjust the position of the button |
(15 is margin)
See the scroll bar on the right side?
We’re done here. Now we have a smooth scrollable content view
Ahhhhh!!!! One more thing here
The initial height of UIWebView
must not be 0
, e.g.
ViewController.m
1 | _webView = [[UIWebView alloc] initWithFrame:CGRectMake(0, 0, 320, 0)]; // <--- if height is 0 |
The result will be
This is due to document.getElementById('main-wrapper').offsetHeight;
return a wrong value, and I don’t know why (if you know please comment), this took me 1 hours++ to figure out, at lease you must put 1
for the height.