Question

In my iOS app, the interface is created by programmatically adding multiple UIViews to the UIViewController that is currently on the screen. Currently the app works in the portrait orientation, but I want to add the ability to work in landscape. I want to change the interface when the app is put in the landscape orientation. Hoe can I do this?

P.S. I can't use the interface builder

Était-ce utile?

La solution

You need to watch for the orientation to change via the NSNotificationCenter then handle it.

[[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications];
[[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(orientationChanged:)
                                                 name:UIDeviceOrientationDidChangeNotification
                                               object:nil];

Then create the method to handle the rotation

- (void)orientationChanged:(NSNotification *)notification
{
    // Get device orientation
    UIDeviceOrientation deviceOrientation   = [UIDevice currentDevice].orientation;

    if (UIDeviceOrientationIsPortrait(deviceOrientation))
    {
        // Portrait
    }
    else if(UIDeviceOrientationIsLandscape(deviceOrientation))
    {
        // Landscape
    }
}
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top