iPhone - 自动调整 UIWebView 内容大小不适合框架
我正在 viewDidLoad 方法中生成一个 UIWebView,其尺寸很小(比如说 50x70)。然后我把它放入一个超级 UIView 中。
我想让它的内容适合 webView 框架。为此,我写道:
oneView = [[UIWebView alloc] initWithFrame:CGRectMake(x, y, W, H)];
oneView.backgroundColor = [UIColor whiteColor];
oneView.autoresizingMask = UIViewAutoresizingFlexibleWidth;
oneView.scalesPageToFit = YES;
oneView.autoresizesSubviews = YES;
[self.view addSubview:oneView];
[oneView loadRequest:/*some request*/];
但是这样做,网页的大小不会调整为 UIWebView 的框架。 它似乎被缩放到其他东西,比超级视图的框架更小,并且比网络视图的框架更宽。
如果我将 webview 框架大小设置为整个超级视图的大小,就可以了。
我如何强制网页内容(真实的 www 内容)适合“缩小的” UIWebView 的框架?
I'm generating an UIWebView into my viewDidLoad method, with a tiny size (let's say something like 50x70). And then I put it into a super UIView.
I'd like to make its content fit the webView frame. To do this, I wrote :
oneView = [[UIWebView alloc] initWithFrame:CGRectMake(x, y, W, H)];
oneView.backgroundColor = [UIColor whiteColor];
oneView.autoresizingMask = UIViewAutoresizingFlexibleWidth;
oneView.scalesPageToFit = YES;
oneView.autoresizesSubviews = YES;
[self.view addSubview:oneView];
[oneView loadRequest:/*some request*/];
But doing this, the web page is not resized to the frame of the UIWebView.
It seems to be scaled to something else, smaller than the superview's frame, and wider than the webview's one.
If I set the webview frame size to the whole superview's size, it's ok.
How may I force the web content (real www content) to fit the frame of the "reduced" UIWebView ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
答案是:你已经在这样做了,但是有限制。
看下面的截图:
截图 1,scalesPageToFit YES 和 NO
两个 webview 具有相同的宽度。下一页的页面非常适合。
Screeshot 2,scalesPageToFit 均为 YES,但设置了较小的宽度
两个 webview 都尝试适应页面,但不会,因为有大小限制。
The answer is: you are already doing this but there are limits.
Look at the following screenshots:
Screeshot 1, scalesPageToFit YES and NO
Both webview have the same width. In the lower one the page fits perfectly.
Screeshot 2, scalesPageToFit both YES but smaller widths set
Both webview try to fit the page but it won't as there is a size limit.
试试这个。这对我有用。
Try this.That's working for me.
对于那些遇到同样问题的人,您可以关闭 UIWebView 原生滚动
并添加一个单独的
scrollView
来处理滚动和缩放,而不是webView
的原生滚动。接下来实现
设置正确的
webView
框架。希望这对某人有帮助。良好的编码!
For those, who faced same problem, you can turn off the UIWebView native scrolling by
and add a separate
scrollView
which will handle scrolling and zooming instead ofwebView
's native scroll.Next implement
to set proper
webView
frame.Hope this helps someone. Good coding!
在 web 视图中使用
sizethatfits()
可以解决您的问题。Use
sizethatfits()
in the webview which would resolve your issue.您可以执行以下操作:
这将使您的视图内容随着视图的增长而增长。内容将尽可能大,同时仍然适合 UIWebView 并且不变形。
此属性还有其他选项,您会在 iOS 视图编程指南
请注意,自动调整大小您设置的掩码将使视图本身仅在其超级视图增长时增长。如果在创建小 UIWebView 时 self.view 已经很大并且 self.view 没有增长,则 UIWebView 将不会增长。您可能已经意识到这一点,但我添加它只是为了以防万一,因为我们在这段代码中看不到 self.view 的框架。
希望这有帮助。
You can do the following:
That will make the content of your view grow as your view grows. The content will grow as big as possible while still fitting within the UIWebView and without distortion.
There are other options for this property, and you will find a pretty good explanation of the more complicated ones, along with pictures showing the effect of each, in the View Programming Guide for iOS
Note that the autoresizing mask you set will make the view itself grow only when its superview grows. If self.view is already big when the small UIWebView is created and self.view does not grow, the UIWebView won't be growing. You're probably aware of this, but I'm adding it just in case, since we can't see self.view's frame in this snippet of code.
Hope this is helpful.
对于 Swift 2.2 我已经实现了相同的目标
For Swift 2.2 I have achieved the same with
对于 Swift 3,我已经实现了同样的目标
For Swift 3, I have achieved the same with