Titanium Community Questions & Answer Archive

We felt that 6+ years of knowledge should not die so this is the Titanium Community Questions & Answer Archive

iPad orientationchange problems..

What this code is supposed to do is change those properties when the iPad switches orientation, and it does in the Simulator and on my iPad. But the problem is that it switches whenever there is any slight movement. Like say I have my iPad in Portrait mode and I'm just walking around it switches back and forth from the Portrait and Landscape settings I specified, even though the device hasn't been turned on it's side to Landscape.

Here is my code..

//////////ORIENTATION
win1.orientationModes = [ 
    Titanium.UI.PORTRAIT, 
    Titanium.UI.UPSIDE_PORTRAIT, 
    Titanium.UI.LANDSCAPE_LEFT, 
    Titanium.UI.LANDSCAPE_RIGHT, 
    Titanium.UI.FACE_UP, 
    Titanium.UI.FACE_DOWN
];

function getOrientation(o) {
    switch (o) {
        case Titanium.UI.PORTRAIT: return 'portrait';
        case Titanium.UI.UPSIDE_PORTRAIT: return 'portrait';
        case Titanium.UI.LANDSCAPE_LEFT: return 'landscape';
        case Titanium.UI.LANDSCAPE_RIGHT: return 'landscape';
    }
}

Ti.Gesture.addEventListener('orientationchange',function(e){
    var orientation = getOrientation(e.orientation);
    if (orientation == 'portrait') {
        header.bottom = 800;
        header.left = 34;
        view.left = 34;
        view.bottom = 0;
        view.height = 800;
        dateDisplay.bottom = 675;
        dateDisplay.left = 635;

    } else {
        header.bottom = 550;
        header.left = 300;
        view.height = 550;
        view.left = 300;
        dateDisplay.bottom = 423;
        dateDisplay.left = 900;

    }

});

Has anyone else had problems with this or did I just do something wrong. Any help would be great! Thanks!

— asked May 3rd 2010 by Ross Waycaster
  • ipad
  • landscape
  • mobile
  • orientation
  • orientationchange
  • portrait
  • transition
0 Comments

4 Answers

  • @Juan Pons Thanks, I did that but it didn't fix the problem.

    FIXED IT!
    However I did find a solution. I added an if landscape and it worked.. check the code below.

    //////////ORIENTATION
    win1.orientationModes = [ 
        Titanium.UI.PORTRAIT, 
        Titanium.UI.UPSIDE_PORTRAIT, 
        Titanium.UI.LANDSCAPE_LEFT, 
        Titanium.UI.LANDSCAPE_RIGHT
    ];
    
    function getOrientation(o) {
        switch (o) {
            case Titanium.UI.PORTRAIT: return 'portrait';
            case Titanium.UI.UPSIDE_PORTRAIT: return 'portrait';
            case Titanium.UI.LANDSCAPE_LEFT: return 'landscape';
            case Titanium.UI.LANDSCAPE_RIGHT: return 'landscape';
        }
    }
    
    Ti.Gesture.addEventListener('orientationchange',function(e){
        var orientation = getOrientation(e.orientation);
        if (orientation == 'portrait') {
            header.bottom = 800;
            header.left = 34;
            view.left = 34;
            view.bottom = 0;
            view.height = 800;
            dateDisplay.bottom = 675;
            dateDisplay.left = 635;
    
        } else if (orientation == 'landscape') {
            header.bottom = 550;
            header.left = 300;
            view.height = 550;
            view.left = 300;
            dateDisplay.bottom = 423;
            dateDisplay.left = 900;
    
        } else {
        //do nothing
        }
    
    });
    

    Hope this helps someone! It works perfectly now!

    — answered May 3rd 2010 by Ross Waycaster
    permalink
    3 Comments
    • Does the orientation layout feel smooth? Or does it flicker after changing the orientation?
      Have a look at this question thanks

      — commented March 24th 2011 by Daniel Tome
    • Thanks for the solution. Today I had a similar problem and this post saved me hours of precious time. The solution works elegantly, without any flickering.

      — commented December 21st 2011 by Nahum Romero
    • I still can't get this to work. Here is my code. Can you see anything I might be doing wrong?

      Ti.Gesture.isLandscape = function(orient) {
        orient = orient || Ti.UI.orientation;
        return orient == Ti.UI.LANDSCAPE_LEFT || orient == Ti.UI.LANDSCAPE_RIGHT;
      };
      
      Ti.Gesture.isPortrait = function(orient) {
        orient = orient || Ti.UI.orientation;
        return orient == Ti.UI.PORTRAIT || orient == Ti.UI.UPSIDE_PORTRAIT;
      };
      
      Ti.Gesture.addEventListener('orientationchange', function(e){  
        if(Ti.Gesture.isLandscape(e.orientation)) {
          //Landscape ---                 
              ...
        }else if(Ti.Gesture.isPortrait(e.orientation)){
          //Portrait ---
              ...
        }else{
            //Do nothing ---
        }
      })
      

      — commented May 8th 2012 by Clifton Labrum
  • Ross,

    I've seen similar issues, and I believe that is has to do with the Face_Up and Face_Down events.

    Try removing these two lines from your orientationModes and see if that helps

    Titanium.UI.FACE_UP, 
    Titanium.UI.FACE_DOWN
    
    — answered May 3rd 2010 by Juan Pons
    permalink
    0 Comments
  • I had the same issue. I think the iPad fires orientationchange events for every ever so slight change (that's how you roll an airplane in x-plane). This would explain why your code for "landscape" was executing every time a change event came in, before you added the "if statement"…

    — answered May 8th 2010 by Iko Knyphausen
    permalink
    0 Comments
  • Shake also triggers orientationchange maybe the root cause.
    http://developer.appcelerator.com/question/128920/shake-also-triggers-orientationchange

    — answered May 30th 2012 by Abaw Chen
    permalink
    0 Comments
The ownership of individual contributions to this community generated content is retained by the authors of their contributions.
All trademarks remain the property of the respective owner.