iPAD - Change UIImageView's background on Orientation change

Discussion in 'iOS Programming' started by kamy, Jul 28, 2011.

  1. macrumors member

    Joined:
    Jul 27, 2011
    #1
    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!
     
  2. djEsh, Aug 3, 2011
    Last edited: Aug 3, 2011

    macrumors newbie

    Joined:
    Aug 3, 2011
    #2
    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");
    }

    }
     
  3. thread starter macrumors member

    Joined:
    Jul 27, 2011

Share This Page