1

iOS4.2 When my app launches I display an image that is visible until my webview loads. After the webview loads that image is set to hidden. How do I do the following: If portrait, then display DefaultPortrait.png if Landscape the display DefaultLandscape.png?

if Device is Portrait then display

self.view.backgroundColor = [UIColor blackColor];
CGRect myImageRect = CGRectMake(0.0f, 0.0f, 320.0f, 480.0f);
myImage = [[UIImageView alloc] initWithFrame:myImageRect];
[myImage setImage:[UIImage imageNamed:@"Default2.png"]];
myImage.opaque = YES; // explicitly opaque for performance
[self.view addSubview:myImage];
[myImage release];

OR

if Device is landscape then display

CGRect myImageRect = CGRectMake(0.0f, 0.0f, 480f, 320.0f);
myImage = [[UIImageView alloc] initWithFrame:myImageRect];
[myImage setImage:[UIImage imageNamed:@"Default2L.png"]];
myImage.opaque = YES; // explicitly opaque for performance
[self.view addSubview:myImage];
[myImage release];
user864465
  • 83
  • 1
  • 8
  • Just try my method. Put in the viewDidRotateMethod in ur code. This function will be called whenever the device will be rotated.. – Hadi Jul 30 '11 at 07:53

3 Answers3

3

You can use the didRotateFromInterfaceOrientation to implement your code. Now here when the orientation will be changed from portrait implement the code u wanna display when the device is in landscape mode and vice versa. Hope it solves your problem.

    - (void) didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation{
    if ((fromInterfaceOrientation == UIInterfaceOrientationPortrait) || (fromInterfaceOrientation == UIInterfaceOrientationPortraitUpsideDown)) {

        // your method for landscape..

    }
    if ((fromInterfaceOrientation == UIInterfaceOrientationLandscapeRight) || (fromInterfaceOrientation == UIInterfaceOrientationPortraitUpsideDown)) {

        //your method for portrait
    }
}
Hadi
  • 1,212
  • 2
  • 17
  • 31
0

You can use this at the beginning to get your current orientation:

   UIInterfaceOrientation currentOrientation = [[UIApplication sharedApplication] statusBarOrientation] 

and then use this:

 if ((currentOrientation == UIInterfaceOrientationPortrait) || (currentOrientation == UIInterfaceOrientationPortraitUpsideDown)) {
self.view.backgroundColor = [UIColor blackColor];
CGRect myImageRect = CGRectMake(0.0f, 0.0f, 320.0f, 480.0f);
myImage = [[UIImageView alloc] initWithFrame:myImageRect];
[myImage setImage:[UIImage imageNamed:@"Default2.png"]];
myImage.opaque = YES; // explicitly opaque for performance
[self.view addSubview:myImage];
[myImage release];

    }
   else if ((currentOrientation == UIInterfaceOrientationLandscapeRight) || (currentOrientation == UIInterfaceOrientationLandscapeLeft)) {
self.view.backgroundColor = [UIColor blackColor];
CGRect myImageRect = CGRectMake(0.0f, 0.0f, 320.0f, 480.0f);
myImage = [[UIImageView alloc] initWithFrame:myImageRect];
[myImage setImage:[UIImage imageNamed:@"Default2.png"]];
myImage.opaque = YES; // explicitly opaque for performance
[self.view addSubview:myImage];
[myImage release];
}
Oscar Gomez
  • 18,436
  • 13
  • 85
  • 118
  • http://earthHappens.com/eHap.html I put myviewController .h .m file on my server. I get no error codes but my image does not change when the orientation does. I'm stumped – user864465 Jul 30 '11 at 06:14
  • Thanks, I've chosen to not do what I had originally intended to do. – user864465 Jul 30 '11 at 18:50
0

If you want to display same image in both mode then first u declare a Boolean type variable in app delegate file and define in app delegate .m file. IF portrait mode the variable true and when landscape then false. Now In

_(void)viewDidLoad{
              if (Boolean variable == TRUE ){
                                         //display portrait mode }
                 else 
                      //display landscape mode

}

ram
  • 1,193
  • 1
  • 15
  • 27
  • 2 different images Default2.png and Default2L.png – user864465 Jul 30 '11 at 05:45
  • Display Default2.png when in portrait and display Default2L.png when in landscape. – user864465 Jul 30 '11 at 06:26
  • earthHappens.com/eHap.html I put myviewController .h .m file on my server. I get no error codes but my image does not change when the orientation does. I'm stumped – user864465 Jul 30 '11 at 06:26
  • in app delegate.h file u declare a boolean type variable and u check ur device can check orientation change or not – ram Jul 30 '11 at 06:34
  • in appdelegate.h file u declare as Bool IsLandscape; in app delegate.m file u synthesize it. And in rotation function u define as if ((fromInterfaceOrientation == UIInterfaceOrientationPortrait) || (fromInterfaceOrientation == UIInterfaceOrientationPortraitUpsideDown)) { IsLandScape = False; } if ((fromInterfaceOrientation == UIInterfaceOrientationLandscapeRight) || (fromInterfaceOrientation == UIInterfaceOrientationPortraitUpsideDown)) { IsLandScape=TRUE; } } – ram Jul 30 '11 at 06:55
  • Thanks, I've chosen to not do what I had originally intended to do. – user864465 Jul 30 '11 at 18:49