Monday, June 3, 2013

UIWebView scalesPageToFit example in Objective C (iOS).


UIWebView scalesPageToFit

A Boolean value determining whether the webpage scales to fit the view and the user can change the scale.

@property(nonatomic) BOOL scalesPageToFit

Discussion of [UIWebView scalesPageToFit]
If YES, the webpage is scaled to fit and the user can zoom in and zoom out. If NO, user zooming is disabled. The default value is NO.

UIWebView scalesPageToFit example.
- (void)viewDidLoad {
    [super viewDidLoad];
    webView = [[UIWebView alloc] init];
    webView.scalesPageToFit = YES;
    webView.backgroundColor = [UIColor blackColor];

    self.view.alpha = 1;
    self.view.backgroundColor = [UIColor whiteColor];
}

Example of [UIWebView scalesPageToFit].
view = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 200.0f, 400.0f)];
webViewT = [[UIWebView alloc] initWithFrame:view.bounds];
webViewT.delegate = self;
webViewT.scalesPageToFit = YES;
NSURL* url = [NSURL URLWithString:@"http://www.google.com/"];
[webViewT loadRequest:[NSURLRequest requestWithURL:url]];

In Some of cases this types of problem Generated. At that time java script use for control of Zoom of UIWebView, use following code.

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

UIWebView scalesPageToFit example.
In my experience, you need to set scalesPageToFit before the UIWebView loads. This means setting before viewDidLoad etc. What I do is set it in "shouldStartLoadWithRequest"

- (BOOL)webView:(UIWebView *)theWebView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType
{
    //make sure that the page scales when it is loaded :-)
    theWebView.scalesPageToFit = YES;
    return YES;
}

End of UIWebView scalesPageToFit example article.