Search code examples
objective-ciosipaddevice-orientation

iPad - Change image depending on device orientation (Portrait or Landscape)


Inside my viewDidLoad method, I am applying a "loading image" that appears until a webView had completed loading. I would like to specify a different loading image depending on which orientation (portrait or landscape) the iPad is in.

I found this page which is what I based my code from.

My code is shown below. (this is all contained within my viewDidLoad method). This only works in Portrait mode at the moment. Any idea why my landscape code isn't being called? Please help!

//detect if iPad    
if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) {
//if in Portrait Orientation
            if([[UIApplication sharedApplication] statusBarOrientation] == UIInterfaceOrientationPortrait) {
                loadingImage = [UIImage imageNamed:@"webView-load-image-Portrait~ipad.png"];
                loadingImageView = [[UIImageView alloc] initWithImage:loadingImage];
                loadingImageView.animationImages = [NSArray arrayWithObjects:
                                                    [UIImage imageNamed:@"webView-load-image-Portrait~ipad.png"],
                                                    nil];
            }
//if in Landscape Orientation
            if([[UIApplication sharedApplication] statusBarOrientation] == UIInterfaceOrientationLandscapeLeft
               || [[UIApplication sharedApplication] statusBarOrientation] == UIInterfaceOrientationLandscapeRight) {
                loadingImage = [UIImage imageNamed:@"webView-load-image-Landscape~ipad.png"];
                loadingImageView = [[UIImageView alloc] initWithImage:loadingImage];
                loadingImageView.animationImages = [NSArray arrayWithObjects:
                                                    [UIImage imageNamed:@"webView-load-image-Landscape~ipad.png"],
                                                    nil];
            }

        }

UPDATE

I know you're supposed to be able to use image file name modifiers so that the device will automatically pull the correct image. I have tried putting webView-load-image.png in my app images folder with all the correct extensions (and removing the -Portrait~ipad from the file name):

  • webView-load-image~iphone.png
  • webView-load-image@2x~iphone.png
  • webView-load-image-Landscape~ipad.png
  • webView-load-image-Landscape@2x~ipad.png
  • webView-load-image-Portrait~ipad.png
  • webView-load-image-Portrait@2x~ipad.png

and it is still not working :(


Solution

  • I figured it out myself. Here's what worked for me in case anyone else is having the same issue:

    //ViewController.h
    @interface ViewController : GAITrackedViewController {
        // ...other code
        UIImage *loadingImage;
    }
    
    @property(nonatomic, strong) UIImageView *loadingImageView;
    
    
    //ViewController.m
        - (void)viewDidLoad
        {
            [super viewDidLoad];
    
            ...bunch of other code....
    
        // Subscribe to device orientation notifications, and set "orientation" - will return a number 1-5, with 5 being "unknown".
            [[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications];
            UIInterfaceOrientation orientation = [UIDevice currentDevice].orientation;
    
        //**************** Start Add Static loading image  ****************/
            //if iPhone
            if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone) {
            loadingImage = [UIImage imageNamed:@"webView-load-image~iphone.png"];
            loadingImageView = [[UIImageView alloc] initWithImage:loadingImage];
                loadingImageView.animationImages = [NSArray arrayWithObjects:
                                                    [UIImage imageNamed:@"webView-load-image~iphone.png"],
                                                    nil];
            }
    
           if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) {
               if(orientation == 3 || orientation == 4) {
                   loadingImage = [UIImage imageNamed:@"webView-load-image-Landscape~ipad.png"];
                   loadingImageView = [[UIImageView alloc] initWithImage:loadingImage];
                   loadingImageView.animationImages = [NSArray arrayWithObjects:
                                                       [UIImage imageNamed:@"webView-load-image-Landscape~ipad.png"],
                                                       nil];
                   NSLog(@"Set webView-load-image iPhone Landscape");
    
               }
               if(orientation == 5) {
                   NSLog(@"Set webView-load-image UNKNOWN");
               }
               if(orientation == 1) {
                    loadingImage = [UIImage imageNamed:@"webView-load-image-Portrait~ipad.png"];
                    loadingImageView = [[UIImageView alloc] initWithImage:loadingImage];
                    loadingImageView.animationImages = [NSArray arrayWithObjects:
                                                        [UIImage imageNamed:@"webView-load-image-Portrait~ipad.png"],
                                                        nil];
                    NSLog(@"Set webView-load-image iPad Portrait");
                }
    
    
            }
    
            [self.view addSubview:loadingImageView];
            // End Add Static loading image