Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

kamy

macrumors member
Original poster
Jul 27, 2011
44
0
Hi experts,

When i change the orientation say from Portrait to Landscape mode, how do i change the background image of a UIImageView which is inside a view?

I have two images
Login-iPad-Portrait.png - 768 × 1004 and
Login-iPad-Landscape.png - 1024 × 748

So in IB, i set the background of the imageview to 'Login-iPad-Portrait.png'
And in the code, this is what i do

Code:
Code:
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation 
{
	// Return YES for iPAD
	return YES;
}


- (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration
{
    [super willRotateToInterfaceOrientation:toInterfaceOrientation duration:duration];
    if(toInterfaceOrientation == UIInterfaceOrientationPortrait || toInterfaceOrientation == UIInterfaceOrientationPortraitUpsideDown)
	{
        backgroungImage.image = [UIImage imageNamed:@"Login-iPad- Portrait.png"];
		
    }
    else if(toInterfaceOrientation == UIInterfaceOrientationLandscapeRight || toInterfaceOrientation == UIInterfaceOrientationLandscapeLeft)
	{
       backgroungImage.image = [UIImage imageNamed:@"Login-iPad-Landscape.png"];
        }
}

But the 'willRotateToInterfaceOrientation' event is never triggered, even as i change the orientation. Thus the orientation specific image is not updated.

Any pointers to what am doin wrong?

Or is there any other way of doing this?

Thanks in Advance!
 

djEsh

macrumors newbie
Aug 3, 2011
1
0
Try this

You should use orientationChanged instead of willRotateToInterfaceOrientation:

- (void) orientationChanged:(id)object
{


UIDeviceOrientation toInterfaceOrientation = [[UIDevice currentDevice] orientation];

if(toInterfaceOrientation == UIInterfaceOrientationPortrait || toInterfaceOrientation == UIInterfaceOrientationPortraitUpsideDown)
{
UIImage *img = [UIImage imageNamed:mad:"IMG_portrait.jpg"];
[backgroundImage setImage:img];
NSLog(@"IMG_portrait");

}
else if(toInterfaceOrientation == UIInterfaceOrientationLandscapeRight || toInterfaceOrientation == UIInterfaceOrientationLandscapeLeft)
{
UIImage *img = [UIImage imageNamed:mad:"IMG_landscape.jpg"];
[backgroundImage setImage:img];
NSLog(@"IMG_landscape");
}

}
 
Last edited:
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.