How to simply handle orientation changes?
Asked Answered
P

3

6

I'm not talking about doing anything fancy. I'd just like the standard windows and views to rotate when the user rotates the device.

Pleura answered 16/3, 2011 at 8:44 Comment(0)
M
10

You need to tell the window which orientations it should support:

var window = Ti.UI.createWindow({
    orientationModes: [
        Ti.UI.LANDSCAPE_LEFT,
        Ti.UI.LANDSCAPE_RIGHT,
        Ti.UI.PORTRAIT,
        Ti.UI.UPSIDE_PORTRAIT
    ]
});

window.open();

You can then listen on the orientation changes with a listener like so:

Ti.Gesture.addEventListener('orientationchange', function(e) {
    Titanium.API.info('Orientation changed');
});

Edit: I think (though I've never tried it) you can also set this in tiapp.xml, which has the added benefit of applying to all windows automatically.

<orientations device="iphone">
    <orientation>Ti.UI.LANDSCAPE_LEFT</orientation>
    <orientation>Ti.UI.LANDSCAPE_RIGHT</orientation>
    <orientation>Ti.UI.PORTRAIT</orientation>
    <orientation>Ti.UI.UPSIDE_PORTRAIT</orientation>
</orientations>
Maisonette answered 16/3, 2011 at 12:16 Comment(1)
I think you're right about the tiapp.xml config change. That's what I was looking for.Pleura
T
1
Titanium.Gesture.addEventListener('orientationchange', function(e) {
    Titanium.API.info('Gesture Change Detected');
    Ti.App.fireEvent('orientationchange', {eventObject:e});
});
Toxicosis answered 16/3, 2011 at 11:7 Comment(2)
So -- one must take the event, and then re-fire it, in order for the display to rotate?Pleura
this is an approach use to compartmentalize the functionality of a orientation change in each of my views. You do not have to re-fire the eventToxicosis
S
1

this is work on android default. But not work on iphone, so just write this code

var win1 = Ti.UI.createWindow({
   title : 'Tab 1',
 orientationModes: [
        Ti.UI.LANDSCAPE_LEFT,
        Ti.UI.LANDSCAPE_RIGHT,
        Ti.UI.PORTRAIT,
        Ti.UI.UPSIDE_PORTRAIT
    ],
});

win1.open();

Ti.Gesture.addEventListener('orientationchange', function(e) {
    Titanium.API.info(Ti.Gesture.orientation);
});

I, THINK THIS IS USEFUL TO YOU.

Soper answered 7/3, 2012 at 10:52 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.