xcode UIWebView 内容不会在不同 iPhone 尺寸之间缩放
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/26689911/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
UIWebView content not scaling between different iPhone sizes
提问by cptdanko
So I am working on an iOSapp that uses the UIWebView
to serve HTML content. Why UIWebView and not WKWebView? well I want this app to work on iOS7and above. I have just updated my Xcode to Xcode 6.1and I cannot get my webView
to scale to fit the available screen space when changing from the 4 inch screen size to 4.7 and above.
所以我正在开发一个使用来提供 HTML 内容的iOS应用程序UIWebView
。为什么是 UIWebView 而不是 WKWebView?好吧,我希望这个应用程序可以在 iOS7及更高版本上运行。我刚刚将我的 Xcode 更新到Xcode 6.1,webView
当从 4 英寸屏幕尺寸更改为 4.7 及以上时,我无法缩放以适应可用的屏幕空间。
What I am after is to have my app look consistent on all iPhone screen sizes of 4 inch and above as all my HTML5 code that is displayed on the web view is.
我所追求的是让我的应用程序在所有 4 英寸及以上的 iPhone 屏幕尺寸上看起来保持一致,因为我在 Web 视图上显示的所有 HTML5 代码都是如此。
In my Attributes inspector I have set the size to iPhone 4-inch and you can see what the app looks like in simulators for iPhones of size 4-inch and 4.7-inch respectively in the link below, http://imgur.com/gallery/tz7LG/new
在我的属性检查器中,我已将大小设置为 iPhone 4 英寸,您可以在下面的链接中分别查看应用程序在 4 英寸和 4.7 英寸 iPhone 模拟器中的外观, http://imgur.com/画廊/tz7LG/新
How do I get this to scale up appropriately on iPhone screens of different sizes?
如何让它在不同尺寸的 iPhone 屏幕上适当放大?
fyi, I have set the Scale page to Fit to true for my webView. I am building this app using Swift.
仅供参考,我已将我的 webView 的 Scale 页面设置为 Fit 为 true。我正在使用 Swift 构建这个应用程序。
回答by Piyush Mathur
To solve such issue,
为了解决这样的问题,
1) Uncheck "Use Auto layout" and also disable size classes.
1) 取消选中“使用自动布局”并禁用尺寸分类。
2) Open the size inspector to use Autoresizing as directed in the image below on the desired element (in your case its UIWebView).
2) 打开大小检查器以在所需元素(在您的情况下为 UIWebView)上按照下图的指示使用自动调整大小。
Try selecting the bounds as per your requirements. This will surely solve your problem.
尝试根据您的要求选择边界。这肯定会解决您的问题。
回答by Techie4u
Do check that you haven't coded the dimensions in the .m file.
请检查您是否没有在 .m 文件中对尺寸进行编码。
The answer by Piyush Mathuris simple and seems usable in the given case.
Piyush Mathur的答案很简单,在给定的情况下似乎可用。
But if the requirements are complex then you need to "Use Auto Layout".
但是如果需求很复杂,那么你需要“使用自动布局”。
Also make sure that you have set all the needed constraints of the Web-View.
还要确保您已经设置了 Web 视图的所有需要的约束。
The Introduction to Auto-Layoutgives an idea to set constraints to a component which will also give an idea to stretch component (Web-view in your case) based on the device screen size.
在介绍自动布局给出了一个主意组约束到一个组件,它也会给一个想法,根据设备屏幕大小拉伸组件(网络视你的情况)。
回答by cptdanko
You could use Xcode's auto layout tool, which will automatically resize the UIWebView according to the device.
您可以使用 Xcode 的自动布局工具,它会根据设备自动调整 UIWebView 的大小。
回答by Oliver Zhang
I think the best practice is to untick "constraint to margin" when you add the constraints.
我认为最佳做法是在添加约束时取消“对边距的约束”。
Here are three steps: 1. clear all constraints; 2. add the constraints back in and untick "constrain to margin"; 3. set all four constraints to 0.
以下是三个步骤: 1. 清除所有约束;2. 重新添加约束并取消勾选“约束到边距”;3. 将所有四个约束设置为 0。
Or if you are familiar with the Xcode, this can be done in one step.
或者,如果您熟悉 Xcode,这可以一步完成。
回答by Arkady
What helped me is a creation of 4 constraints:
帮助我的是创建 4 个约束: