iPhone UIWebView width does not fit after zooming operation + UIInterfaceOrientation change

前端 未结 8 1539
庸人自扰
庸人自扰 2020-12-02 19:20

I created a bare bones iPhone app with a UIWebView (Scales Page to Fit = YES, shouldAutorotateToInterfaceOrientation = YES) and loaded a webpage, e.g. https://stackoverflow.

8条回答
  •  悲哀的现实
    2020-12-02 20:02

    So apparently I didn't use the solution by M Penades in the end (and forgot to update this post! sorry).

    What I did was to resize the entire document (and change my font-size to keep things proportionate). That apparently fixed the issue.

    However, my UIWebView is only for loading my own HTML & CSS from the iOS filesystem - if you're building a general purpose web browser, this trick may not work as well.

    ViewController.m

    - (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration {
        switch (toInterfaceOrientation) {
            case UIInterfaceOrientationPortraitUpsideDown:
            case UIInterfaceOrientationPortrait:
                if ((UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad)) {
                    [webview stringByEvaluatingJavaScriptFromString:@"document.body.className = 'ppad'"];
                } else {
                    [webview stringByEvaluatingJavaScriptFromString:@"document.body.className = 'pphone'"];
                }
                break;
            default:
                if ((UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad)) {
                    [webview stringByEvaluatingJavaScriptFromString:@"document.body.className = 'lpad'"];
                } else {
                    [webview stringByEvaluatingJavaScriptFromString:@"document.body.className = 'lphone'"];
                }
                break;
        }
    }
    

    And app.css

    html>body.pphone { font-size:12px; width: 980px; }
    html>body.lphone { font-size:18px; width: 1470px; }
    html>body.ppad { font-size:12px; width: 768px; }
    html>body.lpad { font-size:15.99999996px; width: 1024px; }
    

    Gist at https://gist.github.com/d6589584944685909ae5

提交回复
热议问题