cain
cain

Reputation: 1048

Fit content to width in UIWebView

I have a UIWebView that is loading data that comes from a web service. It can be sent a pdf, a tiff, text, or multiple other types of data. That much of it works great. The problem I am having is, on the iPhone I would like the content to fill the UIWebView (scaled down if needed) without having to scroll horizontally.

I'm seen multiple questions about this, but it doesn't seem like the answers to several of them actually work.

I have selected Scales Page To Fit for the webview in the storyboard builder. This does scale it down a great bit, but I still have to scroll to see the document.

As recommended in another question, I have tried using javascript to scale my content in the webViewDidFinishLoad method with:

NSString *jsCommand = [NSString stringWithFormat:@"document.body.style.zoom = %f;",zoomAmount];
[webViewF stringByEvaluatingJavaScriptFromString:jsCommand];

But this doesn't seem like a good way to do it. It flickers to re-adjust the size, and I'm not sure of the best way to figure out the zoom ratio anyway.

Does anyone know the correct way to size content in a UIWebView so that it fills the screen horizontally by default?

Upvotes: 3

Views: 5465

Answers (2)

无夜之星辰
无夜之星辰

Reputation: 6148

-(void)webViewDidFinishLoad:(UIWebView *)webView {
    [webView sizeToFit];
}

Upvotes: 0

Walter
Walter

Reputation: 5887

Use a viewport

<meta name="viewport" content="width=320; initial-scale=1.0; minimum-scale=1.0;"/>

That code goes within the head tag of the html that you create.

Upvotes: 2

Related Questions