Iphone UIWebView在旋转后调整文本大小:寻找神奇bug或我的愚蠢的解释

Iphone UIWebView在旋转后调整文本大小:寻找神奇bug或我的愚蠢的解释,iphone,cocoa-touch,uiwebview,webkit,Iphone,Cocoa Touch,Uiwebview,Webkit,所以我在我的应用程序中有UIWebView,我加载了一些html(文章),添加了一些CSS使一切变得更好。这很简单明了,一切都很完美,直到我尝试添加横向阅读功能,然后我注意到UIWebView忽略了我在HTML中输入的任何css“字体大小”,并在旋转到横向模式后大幅增加字体大小 我花了大约4-5个小时(我对编写Iphone应用程序还不熟悉,但在做正确的事情时我很固执)试图修复它。尝试了很多很多配置选项-什么都没有 今晚发生了神奇的事情。只要看看资料来源: 错误背景: r = CGRectMake

所以我在我的应用程序中有UIWebView,我加载了一些html(文章),添加了一些CSS使一切变得更好。这很简单明了,一切都很完美,直到我尝试添加横向阅读功能,然后我注意到UIWebView忽略了我在HTML中输入的任何css“字体大小”,并在旋转到横向模式后大幅增加字体大小

我花了大约4-5个小时(我对编写Iphone应用程序还不熟悉,但在做正确的事情时我很固执)试图修复它。尝试了很多很多配置选项-什么都没有

今晚发生了神奇的事情。只要看看资料来源:

错误背景:

r = CGRectMake(0.0, 0.0, 480.0, 320.0);
[adView.view removeFromSuperview];
if (!isFullScreen) {
    minus = 50 + minus;
    [controlsView setFrame:CGRectMake(0, r.size.height - 50, r.size.width, 50)];        
} else {
    minus = 20; 
}

[textView setFrame:CGRectMake(0, 0, r.size.width, r.size.height - minus)];  
[scrollView setFrame:CGRectMake(0, 0, r.size.width, r.size.height - minus)];
r = CGRectMake(0.0, 0.0, 480.0, 320.0);
[adView.view removeFromSuperview];
if (!isFullScreen) {
    minus = 50 + minus;
    [controlsView setFrame:CGRectMake(0, r.size.height - 50, r.size.width, 50)];        
} else {
    minus = 20; 
}
[textView setFrame:CGRectMake(0, 0, r.size.width, r.size.height - minus)];  
[scrollView setFrame:CGRectMake(0, 0, r.size.width, r.size.height - minus)];

if (!isFullScreen) {
    minus = 1 + minus;
} else {
    minus = 20+1;   
}
[textView setFrame:CGRectMake(0, 0, r.size.width, r.size.height - minus)];  
[scrollView setFrame:CGRectMake(0, 0, r.size.width, r.size.height - minus)];
横向固定(字体大小不再改变):

r = CGRectMake(0.0, 0.0, 480.0, 320.0);
[adView.view removeFromSuperview];
if (!isFullScreen) {
    minus = 50 + minus;
    [controlsView setFrame:CGRectMake(0, r.size.height - 50, r.size.width, 50)];        
} else {
    minus = 20; 
}

[textView setFrame:CGRectMake(0, 0, r.size.width, r.size.height - minus)];  
[scrollView setFrame:CGRectMake(0, 0, r.size.width, r.size.height - minus)];
r = CGRectMake(0.0, 0.0, 480.0, 320.0);
[adView.view removeFromSuperview];
if (!isFullScreen) {
    minus = 50 + minus;
    [controlsView setFrame:CGRectMake(0, r.size.height - 50, r.size.width, 50)];        
} else {
    minus = 20; 
}
[textView setFrame:CGRectMake(0, 0, r.size.width, r.size.height - minus)];  
[scrollView setFrame:CGRectMake(0, 0, r.size.width, r.size.height - minus)];

if (!isFullScreen) {
    minus = 1 + minus;
} else {
    minus = 20+1;   
}
[textView setFrame:CGRectMake(0, 0, r.size.width, r.size.height - minus)];  
[scrollView setFrame:CGRectMake(0, 0, r.size.width, r.size.height - minus)];
正如您所看到的,我只添加了相同的代码一次,并将边距(减)增加了一个点。并再次执行了setFrame(不,它不会修复仅使用一组setFrame调整大小的问题)

我很高兴它能工作,但我想知道如何和为什么,所以我可以“正确的方式”完成它,因为现在代码看起来很愚蠢,在应用程序的底部有1点高的边距


不要问我为什么要再次复制粘贴相同的代码。

您可能只需要阅读以下内容

类似地,如果仅指定 视口宽度、高度和初始值 规模是推断出来的。图3-15显示了 同一网页在 视口宽度设置为320。 请注意,纵向方向 呈现的与图中相同 3-14,但景观方向 保持320像素的宽度,这 更改初始比例并具有 当用户 更改横向方向。


神奇的解决办法是:

<meta name='viewport' content='width=device-width; initial-scale=1.0; maximum-scale=1.0;'>


要是我这周前知道就好了。。这么多的时间,这么多的神经都失去了..

我试过你的案子,但没有成功

为我工作的解决方案是:

[webView重新加载]

还提到了如何在CSS中禁用文本的自动大小调整,这解决了我的问题

html{
-webkit文本大小调整:无;/*从不自动调整文本大小*/
}

使用前两个答案,CSS属性:

html {
    -webkit-text-size-adjust: none; /* Never autoresize text */
}
和元标记:

<meta name='viewport' content='width=device-width; initial-scale=1.0; maximum-scale=1.0;'>
使用UIWebViewDelegate中的webViewDidFinishLoad:

- (void)webViewDidFinishLoad:(UIWebView *)webView{

    NSString *javascript = @"var style = document.createElement(\"style\"); document.head.appendChild(style); style.innerHTML = \"html{-webkit-text-size-adjust: none;}\";var viewPortTag=document.createElement('meta');viewPortTag.id=\"viewport\";viewPortTag.name = \"viewport\";viewPortTag.content = \"width=320; initial-scale=1.0; maximum-scale=1.0; user-scalable=0;\";document.getElementsByTagName('head')[0].appendChild(viewPortTag);";
    [webView stringByEvaluatingJavaScriptFromString:javascript];

}

我有另一种方法通过添加元素来解决它:

<table>
<tr>
<td>
  [your html content]
</td>
</tr>
</table>

[您的html内容]

您能解释一下在我的情况下如何避免这种情况吗?顺便说一句,我不是在加载外部页面,而是动态创建html,然后将其加载到我的webview中;CSS做到了。我刚刚解决了我的问题。只需将html附加到该标记之后,视口就会按预期调整大小。hi@sniurkst!你能把被接受的答案改成现在正确的答案(7年后!)吗?很高兴这项工作做得很好,但是在使用此解决方案后,您无法在webView上进行收缩和缩放。非常好的修复。为我工作。:)感谢您的回答,这让我发疯。如果您想保留缩放功能,请使用“100%”而不是“无”,根据2016年解决方案: