displaying Wiki mobile page in UIWebView within UIPopoverController

前端 未结 3 1713
孤独总比滥情好
孤独总比滥情好 2020-12-18 15:34

I try to open wiki mobile version webpage by a UIWebView within a UIPopoverController. the problem is, not matter how I set my contentSizeForViewInPopover, or just UIWebView

3条回答
  •  余生分开走
    2020-12-18 15:35

    It would be much more efficient to manipulate the device-width via JavaScript rather than altering the html after it has fully loaded and then reloading the full page with modified html again.

    This should work (and also consider if it's even necessary to change the viewport width):

    - (void)webViewDidFinishLoad:(UIWebView *)aWebView {
        if(aWebView.frame.size.width < aWebView.window.frame.size.width) {
            // width=device-width results in a wrong viewport dimension for webpages displayed in a popover
            NSString *jsCmd = @"var viewport = document.querySelector('meta[name=viewport]');";
            jsCmd = [jsCmd stringByAppendingFormat:@"viewport.setAttribute('content', 'width=%i, initial-scale=1.0, user-scalable=1');", (NSUInteger)aWebView.frame.size.width];
            [aWebView stringByEvaluatingJavaScriptFromString:jsCmd];
        }
        // stop network indicator
        [UIApplication sharedApplication].networkActivityIndicatorVisible = NO;
    }
    

提交回复
热议问题