UIWebView dones't resize correctly when orientation change?

后端 未结 7 2104
伪装坚强ぢ
伪装坚强ぢ 2020-12-02 14:11

I have add a webview,a titleLabel and a coverflowView on a viewcontroller\'s view as its subviews, I want it to change size when the orientation change. I have change the we

7条回答
  •  忘掉有多难
    2020-12-02 14:16

    Just giving a headsup if others have same problem.

    The webview wont resize properly because the HTML is already rendered (iam not sure if this is completely true, might just be a bug in the webview itselfs), but anyway. I made a method that resizes the webview html body width:

    - (void) resizeHtml
    {
         NSString *jsString = [[NSString alloc] initWithFormat:@"document.getElementsByTagName('html')[0].style.width= '%dpx'", (int) _frame.size.width];
    
        [self.webView stringByEvaluatingJavaScriptFromString:jsString];
        [jsString release];
    }
    

    This seems to work just fine on the iphone, but on the ipad i had to do this

    - (void) reloadHtml
    {
        NSString *html = [webView stringByEvaluatingJavaScriptFromString:@"document.documentElement.outerHTML"];
    
        if(html == nil || [html isEqualToString:@""] || [html length] < 100)
            [self.webView reload];
    
        [self.webView loadHTMLString:html baseURL:[NSURL URLWithString:@"some base url"]];
    }
    

    You could also have used [self.webview reload] but that would make another request to the server, and i think that is bad ;-)

    I hope this helps!

提交回复
热议问题