40media How do I lock the orientation to portrait mode in a iPhone Web Application?

How do I lock the orientation to portrait mode in a iPhone Web Application?

I'm building a iPhone Web Application and want to lock the orientation to portrait mode. is this possible? Are there any web-kit extensions to do this?

Please note this is an application written in HTML and JavaScript for Mobile Safari, it is NOT a native application written in Objective-C.

iTune-style apps using C#


how to remove subviews from scrollview?
You can specify CSS styles based on viewport orientation: Target the browser with body[orient="landscape"] or body[orient="portrait"].
iPhone PHP integration
does using try catch block in xcode show error on real device?
for- loop in background thread accesses each index more than once — iPhone dev
Apple's approach to this issue is to allow the developer to change the CSS based on the orientation change but not to prevent re-orientation completely.

Update coordinate property of MKAnnotation on parent viewcontroller after Touch End event?
I found a similar question elsewhere:.
Memory Management Question in Objective C
UITableView frame height animation glitch


This is a pretty hacky solution, but it's at least something(?).

The idea is to use a CSS transform to rotate the contents of your page to quasi-portrait mode.

Here's JavaScript (expressed in jQuery) code to get you started: .
$(document).ready(function () {   function reorient(e) {     var portrait = (window.orientation % 180 == 0);     $("body > div").css("-webkit-transform", !portrait ? "rotate(-90deg)" : "");   }   window.onorientationchange = reorient;   window.setTimeout(reorient, 0); }); 
The code expects the entire contents of your page to live inside a div just inside the body element.

It rotates that div 90 degrees in landscape mode - back to portrait.. Left as an exercise to the reader: the div rotates around its centerpoint, so its position will probably need to be adjusted unless it's perfectly square.. Also, there's an unappealing visual problem.

When you change orientation, Safari rotates slowly, then the top-level div snaps to 90degrees different.

For even more fun, add.
body > div { -webkit-transition: all 1s ease-in-out; } 
to your CSS.

When the device rotates, then Safari does, then the content of your page does.



This answer is not yet possible, but I am posting it for "future generations".

Hopefully, some day we will be able to do this via the CSS @viewport rule:.
@viewport {     orientation: portrait; } 
Spec(in process):
https://drafts.csswg.org/css-device-adapt/#orientation-desc. MDN:
https://developer.mozilla.org/en-US/docs/Web/CSS/@viewport/orientation. Based on the MDN browser compatibility table and the following article, looks like there is some support in certain versions of IE and Opera:
http://menacingcloud.com/?c=cssViewportOrMetaTag. This JS API Spec also looks relevant:
https://w3c.github.io/screen-orientation/. I had assumed that because it was possible with the proposed @viewport rule, that it would be possible by setting orientation in the viewport settings in a meta tag, but I have had no success with this thus far.. Feel free to update this answer as things improve..


The following code was used in our html5 game..
$(document).ready(function () {      $(window)               .bind('orientationchange', function(){                if (window.orientation % 180 == 0){                    $(document.body).css("-webkit-transform-origin", "")                        .css("-webkit-transform", "");                               }                 else {                                       if ( window.orientation > 0) { //clockwise                      $(document.body).css("-webkit-transform-origin", "200px 190px")                        .css("-webkit-transform",  "rotate(-90deg)");                      }                    else {                      $(document.body).css("-webkit-transform-origin", "280px 190px")                        .css("-webkit-transform",  "rotate(90deg)");                     }                }            })           .trigger('orientationchange');  }); 


I came up with this CSS only method of rotating the screen using media queries.

The queries are based on screen sizes that I found here.

480px seemed to be a good as no/few devices had more than 480px width or less than 480px height..
@media (max-height: 480px) and (min-width: 480px) and (max-width: 600px) {      html{         -webkit-transform: rotate(-90deg);            -moz-transform: rotate(-90deg);             -ms-transform: rotate(-90deg);              -o-transform: rotate(-90deg);                 transform: rotate(-90deg);         -webkit-transform-origin: left top;            -moz-transform-origin: left top;             -ms-transform-origin: left top;              -o-transform-origin: left top;                 transform-origin: left top;         width: 320px; /*this is the iPhone screen width.*/         position: absolute;         top: 100%;             left: 0     } } 


Screen.lockOrientation() will eventually solve this problem, though at the moment it is only supported in Firefox:. http://www.w3.org/TR/screen-orientation/. https://developer.mozilla.org/en-US/docs/Web/API/Screen.lockOrientation.


Maybe in a new future it will have an out-of-the-box soludion.... As for May 2015,. there is an experimental functionality that does that.

. But it only works on Firefox 18+, IE11+, and Chrome 38+.. However, it does not work on Opera or Safari yet.. https://developer.mozilla.org/en-US/docs/Web/API/Screen/lockOrientation#Browser_compatibility. Here is the current code for the compatible browsers:.
var lockOrientation = screen.lockOrientation || screen.mozLockOrientation || screen.msLockOrientation;  lockOrientation("landscape-primary"); 


I like the idea of telling the user to put his phone back into portrait mode.

Like it's mentioned here: http://tech.sarathdr.com/featured/prevent-landscape-orientation-of-iphone-web-apps/ ...but utilising CSS instead of JavaScript..


// CSS hack to prevent layout breaking in landscape // e.g. 

screens larger than 320px html { width: 320px; overflow-x: hidden; }
This, or a similar CSS solution, will at least preserve your layout if that is what you are after.

. The root solution is accounting for device's capabilities rather than attempting to limit them.

If the device doesn't allow you the appropriate limitation than a simple hack is your best bet since the design is essentially incomplete.

The simpler the better..


In coffee if anyone needs it..
    $(window).bind 'orientationchange', ->         if window.orientation % 180 == 0             $(document.body).css                 "-webkit-transform-origin" : ''                 "-webkit-transform" : ''         else             if window.orientation > 0                 $(document.body).css                     "-webkit-transform-origin" : "200px 190px"                     "-webkit-transform" : "rotate(-90deg)"             else                 $(document.body).css                     "-webkit-transform-origin" : "280px 190px"                     "-webkit-transform" : "rotate(90deg)" 


Click here for a tutorial and working example from my website.. You no longer need to use hacks just to look jQuery Mobile Screen Orientation nor should you use PhoneGap anymore, unless you're actually using PhoneGap.. To make this work in the year 2015 we need:.
  • Cordova (any version though anything above 4.0 is better)
  • PhoneGap (you can even use PhoneGap, plugins are compatible)
And one of these plugins depending on your Cordova version:.
  • net.yoik.cordova.plugins.screenorientation (Cordova < 4)
cordova plugin add net.yoik.cordova.plugins.screenorientation.
  • cordova plugin add cordova-plugin-screen-orientation (Cordova >= 4)
cordova plugin add cordova-plugin-screen-orientation. And to lock screen orientation just use this function:.
To unlock it:.
Possible orientations:.
  • portrait-primary The orientation is in the primary portrait mode..
  • portrait-secondary The orientation is in the secondary portrait mode..
  • landscape-primary The orientation is in the primary landscape mode..
  • landscape-secondary The orientation is in the secondary landscape mode..
  • portrait The orientation is either portrait-primary or portrait-secondary (sensor)..
  • landscape The orientation is either landscape-primary or landscape-secondary (sensor)..

56 out of 100 based on 41 user ratings 616 reviews