PDA

View Full Version : iPAD - Change UIImageView's background on Orientation change




kamy
Jul 28, 2011, 08:06 AM
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:
- (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
Aug 3, 2011, 03:39 PM
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:@"IMG_portrait.jpg"];
[backgroundImage setImage:img];
NSLog(@"IMG_portrait");

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

}

kamy
Aug 4, 2011, 01:34 AM
Thank you sire....that works.