iOS orientation change

August 2, 2015

iOS orientation change

iOS orientation change sometimes required to show bigger clusters of data, like charts or tables and they are better represented in landscape view. To avoid the necessity for user to rotate his device by himself, we can make an app do it by itself instead.

To change device orientation you need to perform a check - which orientation you have right now? To be able to get such information you need to perform this check in - (void)viewDidAppear:(BOOL)animated because prior to this method call the system will return you UIDeviceOrientationUnknown instead of actual orientation enum number.

When you have your orientation value you can perform orientation change. I have wrapped it in an animation block, so this way it would look slower, to give you an example of how you can manipulate animation speed, for instance. Here is complete code sample:

- (void)viewDidAppear:(BOOL)animated {
    [super viewDidAppear:animated];

    UIDeviceOrientation orientation = [UIDevice currentDevice].orientation;
    if (UIDeviceOrientationIsPortrait(orientation)) {
        [UIView animateWithDuration:1 animations:^{
            NSNumber *value = [NSNumber numberWithInt:UIDeviceOrientationLandscapeLeft];
            [[UIDevice currentDevice] setValue:value forKey:@"orientation"];


comments powered by Disqus