如何为 iPhone 设置视口元以正确处理旋转?

发布于 2024-07-30 14:46:26 字数 303 浏览 5 评论 0原文

所以我一直在使用:

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

让我的 HTML 内容在 iPhone 上很好地显示。 它工作得很好,直到用户 将设备旋转到横向模式,其中显示仍限制为 320 像素。

有没有一种简单的方法来指定一个视口,该视口会随着用户更改而变化 设备方向? 或者我必须求助于 Javascript 来处理这个问题?

So I've been using:

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

to get my HTML content to display nicely on the iPhone. It works great until the user
rotates the device into landscape mode, where the display remains constrained to 320px.

Is there a simple way to specify a viewport that changes in response to the user changing the
device orientation? Or must I resort to Javascript to handle that?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(9

江城子 2024-08-06 14:46:27

我想出了一种稍微不同的方法,应该可以在跨平台上工作

http://www.jqui.net/tips-tricks/fixing-the-auto-scale-on-mobile-devices/

到目前为止,我已经在

Samsun Galaxy 2

  • 三星 Galaxy s
  • 上进行了测试Samsung Galaxy s2
  • Samsung Galaxy Note(但必须将 CSS 更改为 800px [见下文]*)
  • Motorola
  • iPhone 4

    • @media 屏幕和(最大宽度:800px){

这是移动开发的一个巨大的口号......

I have come up with a slighly different approach that should work on cross platforms

http://www.jqui.net/tips-tricks/fixing-the-auto-scale-on-mobile-devices/

So far I have tested in on

Samsun galaxy 2

  • Samsung galaxy s
  • Samsung galaxy s2
  • Samsung galaxy Note (but had to change the css to 800px [see below]*)
  • Motorola
  • iPhone 4

    • @media screen and (max-width:800px) {

This is a massive lip forward with mobile development ...

我自己也遇到了这个问题,我希望能够设置宽度,并在旋转时更新它并允许用户缩放和缩放页面(当前的答案提供了第一个,但阻止了后者作为副作用)..所以我想出了一个修复方法,可以使视图宽度保持正确的方向,但仍然允许缩放,尽管它​​不是超级简单。

首先,将以下 Javascript 添加到您正在显示的网页中:

 <script type='text/javascript'>
 function setViewPortWidth(width) {
  var metatags = document.getElementsByTagName('meta');
  for(cnt = 0; cnt < metatags.length; cnt++) { 
   var element = metatags[cnt];
   if(element.getAttribute('name') == 'viewport') {

    element.setAttribute('content','width = '+width+'; maximum-scale = 5; user-scalable = yes');
    document.body.style['max-width'] = width+'px';
   }
  }
 }
 </script>

然后在您的 - (void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation 方法中添加:

float availableWidth = [EmailVC webViewWidth];
NSString *stringJS;

stringJS = [NSString stringWithFormat:@"document.body.offsetWidth"];
float documentWidth = [[_webView stringByEvaluatingJavaScriptFromString:stringJS] floatValue];

if(documentWidth > availableWidth) return; // Don't perform if the document width is larger then available (allow auto-scale)

// Function setViewPortWidth defined in EmailBodyProtocolHandler prepend
stringJS = [NSString stringWithFormat:@"setViewPortWidth(%f);",availableWidth];
[_webView stringByEvaluatingJavaScriptFromString:stringJS];

可以通过修改更多视口内容设置来完成其他调整:

http://www.htmlgoodies.com/beyond/webmaster/toolbox/article.php/3889591/Detect-and-Set-the-iPhone--iPads-Viewport-Orientation-Using-JavaScript -CSS-and-Meta-Tags.htm

另外,我知道你可以为 onresize 或类似的东西放置一个 JS 监听器来触发重新缩放,但这对我有用,因为我是从 Cocoa Touch UI 框架做的。

希望这对某人有帮助:)

I had this issue myself, and I wanted to both be able to set the width, and have it update on rotate and allow the user to scale and zoom the page (the current answer provides the first but prevents the later as a side-effect).. so I came up with a fix that keeps the view width correct for the orientation, but still allows for zooming, though it is not super straight forward.

First, add the following Javascript to the webpage you are displaying:

 <script type='text/javascript'>
 function setViewPortWidth(width) {
  var metatags = document.getElementsByTagName('meta');
  for(cnt = 0; cnt < metatags.length; cnt++) { 
   var element = metatags[cnt];
   if(element.getAttribute('name') == 'viewport') {

    element.setAttribute('content','width = '+width+'; maximum-scale = 5; user-scalable = yes');
    document.body.style['max-width'] = width+'px';
   }
  }
 }
 </script>

Then in your - (void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation method, add:

float availableWidth = [EmailVC webViewWidth];
NSString *stringJS;

stringJS = [NSString stringWithFormat:@"document.body.offsetWidth"];
float documentWidth = [[_webView stringByEvaluatingJavaScriptFromString:stringJS] floatValue];

if(documentWidth > availableWidth) return; // Don't perform if the document width is larger then available (allow auto-scale)

// Function setViewPortWidth defined in EmailBodyProtocolHandler prepend
stringJS = [NSString stringWithFormat:@"setViewPortWidth(%f);",availableWidth];
[_webView stringByEvaluatingJavaScriptFromString:stringJS];

Additional Tweaking can be done by modifying more of the viewportal content settings:

http://www.htmlgoodies.com/beyond/webmaster/toolbox/article.php/3889591/Detect-and-Set-the-iPhone--iPads-Viewport-Orientation-Using-JavaScript-CSS-and-Meta-Tags.htm

Also, I understand you can put a JS listener for onresize or something like to trigger the rescaling, but this worked for me as I'm doing it from Cocoa Touch UI frameworks.

Hope this helps someone :)

⒈起吃苦の倖褔 2024-08-06 14:46:27
<meta name="viewport" content="width=device-width, minimum-scale=1, maximum-scale=1">

支持所有 iPhone、所有 iPad、所有 Android。

<meta name="viewport" content="width=device-width, minimum-scale=1, maximum-scale=1">

suport all iphones, all ipads, all androids.

轮廓§ 2024-08-06 14:46:27

只是想分享,我已经尝试了响应式设计的视口设置,如果我将最大比例设置为 0.8,将初始比例设置为 1 并可缩放为 no,那么我会在纵向模式和 iPad 视图中获得最小视图对于景观:D ...这确实是一个丑陋的黑客,但它似乎有效,我不知道为什么所以我不会使用它,但有趣的结果

<meta name="viewport" content="user-scalable=no, initial-scale = 1.0,maximum-scale = 0.8,width=device-width" />

享受:)

just want to share, i've played around with the viewport settings for my responsive design, if i set the Max scale to 0.8, the initial scale to 1 and scalable to no then i get the smallest view in portrait mode and the iPad view for landscape :D... this is properly an ugly hack but it seems to work, i don't know why so i won't be using it, but interesting results

<meta name="viewport" content="user-scalable=no, initial-scale = 1.0,maximum-scale = 0.8,width=device-width" />

enjoy :)

昨迟人 2024-08-06 14:46:27

为什么不在用户使用 javascript 旋转屏幕时重新加载页面

function doOnOrientationChange()
{
location.reload();
}

window.addEventListener('orientationchange', doOnOrientationChange);

Why not just reload the page when the user rotates the screen with javascript

function doOnOrientationChange()
{
location.reload();
}

window.addEventListener('orientationchange', doOnOrientationChange);
如何视而不见 2024-08-06 14:46:26

我只是想自己解决这个问题,我想出的解决方案是:

<meta name="viewport" content="initial-scale = 1.0,maximum-scale = 1.0" />

这似乎将设备锁定为 1.0 比例,无论其方向如何。 然而,它的副作用是完全禁用用户缩放(捏缩放等)。

Was just trying to work this out myself, and the solution I came up with was:

<meta name="viewport" content="initial-scale = 1.0,maximum-scale = 1.0" />

This seems to lock the device into 1.0 scale regardless of it's orientation. As a side effect, it does however completely disable user scaling (pinch zooming, etc).

苹果你个爱泡泡 2024-08-06 14:46:26

对于任何仍然感兴趣的人:

http://wiki.phonegap.com/w /page/16494815/Preventing-Scrolling-on-iPhone-Phonegap-Applications

从页面:

<meta name="viewport" content="user-scalable=no,width=device-width" />

这会指示 Safari 阻止
用户放大页面
用“捏”手势并修复
视口宽度到宽度
屏幕,无论朝哪个方向
iPhone 已上线。

For anybody still interested:

http://wiki.phonegap.com/w/page/16494815/Preventing-Scrolling-on-iPhone-Phonegap-Applications

From the page:

<meta name="viewport" content="user-scalable=no,width=device-width" />

This instructs Safari to prevent
the user from zooming into the page
with the "pinch" gesture and fixes the
width of the view port to the width of
the screen, which ever orientation the
iPhone is in.

旧时模样 2024-08-06 14:46:26

如果可以的话,您不想失去用户扩展选项。 我喜欢此处的 JS 解决方案。

<script type="text/javascript">
(function(doc) {

    var addEvent = 'addEventListener',
        type = 'gesturestart',
        qsa = 'querySelectorAll',
        scales = [1, 1],
        meta = qsa in doc ? doc[qsa]('meta[name=viewport]') : [];

    function fix() {
        meta.content = 'width=device-width,minimum-scale=' + scales[0] + ',maximum-scale=' + scales[1];
        doc.removeEventListener(type, fix, true);
    }

    if ((meta = meta[meta.length - 1]) && addEvent in doc) {
        fix();
        scales = [.25, 1.6];
        doc[addEvent](type, fix, true);
    }

}(document));
</script>

You don't want to lose the user scaling option if you can help it. I like this JS solution from here.

<script type="text/javascript">
(function(doc) {

    var addEvent = 'addEventListener',
        type = 'gesturestart',
        qsa = 'querySelectorAll',
        scales = [1, 1],
        meta = qsa in doc ? doc[qsa]('meta[name=viewport]') : [];

    function fix() {
        meta.content = 'width=device-width,minimum-scale=' + scales[0] + ',maximum-scale=' + scales[1];
        doc.removeEventListener(type, fix, true);
    }

    if ((meta = meta[meta.length - 1]) && addEvent in doc) {
        fix();
        scales = [.25, 1.6];
        doc[addEvent](type, fix, true);
    }

}(document));
</script>
ま柒月 2024-08-06 14:46:26

您将其设置为无法缩放(最大缩放=初始缩放),因此当您旋转到横向模式时它无法放大。 设置最大比例 = 1.6,它将正确缩放以适应横向模式。

You're setting it to not be able to scale (maximum-scale = initial-scale), so it can't scale up when you rotate to landscape mode. Set maximum-scale=1.6 and it will scale properly to fit landscape mode.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文