g4genuis
g4genuis

Reputation: 45

How to Make Page in UIWebView fit to the WebView

I am trying to display a page in UIWebView in iPad.

It is fitting to browser completely in any browsers but in UIWebView it is not fitting vertically.

Also it is displaying perfectly in iPhone.

Please help.

EDIT :

- (void)webViewDidFinishLoad: (UIWebView *)webView {
    if (webView == myWebView) {
        [loadingView setHidden:YES];

        CGSize contentSize = myWebView.scrollView.contentSize;
        CGSize viewSize = self.view.bounds.size;

        float scale = viewSize.width / contentSize.width;
        if (scale < 0.9) {
            NSLog(@"Zoom out fix for web view: %f", scale);

            webView.scrollView.minimumZoomScale = scale;
            webView.scrollView.maximumZoomScale = scale;
            webView.scrollView.zoomScale = scale;
        }
    }
}

Also have Tried all sorts of combinations in this.

myWebView.contentMode = UIViewContentModeScaleAspectFill | UIViewContentModeScaleAspectFit;
myWebView.autoresizingMask = UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth;
myWebView.scalesPageToFit = YES;
myWebView.autoresizesSubviews = YES;

Upvotes: 0

Views: 2310

Answers (5)

Silviu St
Silviu St

Reputation: 1842

In apple documentation it says this.

You are probably doing something wrong...try comment all the unnecesary code beside setting the content inside webview to localize the problem.

Try make your webview a property, check delegates if they gets called, check if you're filling webview the right way...etc.

Try commenting this :

   CGSize contentSize = myWebView.scrollView.contentSize;
    CGSize viewSize = self.view.bounds.size;

    float scale = viewSize.width / contentSize.width;
    if (scale < 0.9) {
        NSLog(@"Zoom out fix for web view: %f", scale);

        webView.scrollView.minimumZoomScale = scale;
        webView.scrollView.maximumZoomScale = scale;
        webView.scrollView.zoomScale = scale;
    }

and any other stuff to find where your problem is. This is how you should resolve a problem.

Hope it helps :D

Upvotes: 1

Rajesh Loganathan
Rajesh Loganathan

Reputation: 11217

Try this code:

Note: Website must be responsive site to fit in screen as like you expected.

  • Add UIWebViewDelegate delegate to you .h file @interface YourViewController : UIViewController<UIWebViewDelegate>

Code:

- (void)viewDidLoad
{
     [super viewDidLoad];
     //-- Create valid Url by replacing space into %20
     NSString *urlTextEscaped = [UrlString stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
     NSURL *url = [NSURL URLWithString:urlTextEscaped];
     NSLog(@"URL = %@",url);
     NSURLRequest *requestconn = [NSURLRequest requestWithURL:url];
     webview.delegate = self;
     webview.scalesPageToFit =YES;

     //-- Load URL in WebView
     [webview loadRequest:requestconn];
}

Upvotes: 1

valbu17
valbu17

Reputation: 4124

I found this, I'm not sure if you tried already but maybe is worth it to give it shot if you haven't found any solutions for this.

Do it in the - (void)webViewDidFinishLoad:(UIWebView *)webView method apple documentation here!

Then, try using javascript for this.. (you can change the zooming value) btw, only positives values..

NSString *jsCommand = [NSString stringWithFormat:@"document.body.style.zoom = 1.5;"];
[myWebView stringByEvaluatingJavaScriptFromString:jsCommand];

The only thing about this, is that it will happen after the page is loaded but, you can still only show the page after it loads..

I hope it helps!

Upvotes: 2

gabbler
gabbler

Reputation: 13766

I don't know how the webView.xib is loaded, here is an example of loading a webView.xib and by setting its constraints: top space to super view, leading space to super view and center in superview, it works fine.

Upvotes: 2

Yuvrajsinh
Yuvrajsinh

Reputation: 4584

The statement will make your work done.

webView.scalesPageToFit = YES;

This should size the web page to fit the size of the UIWebView, and user then has the option to zoom in and out of the page.

Upvotes: 1

Related Questions