Help: Background image doesn't rotate?

Discussion in 'iOS Programming' started by Zvi, Oct 15, 2008.

  1. Zvi macrumors newbie

    Joined:
    Sep 29, 2008
    #1
    Hi All,

    I have a background image which is created in viewDidLoad
    Code:
    	bgView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"background_2.png"]];
    	[self.view addSubview:bgView];
    	[self.view sendSubviewToBack:bgView];
    
    
    All fine. Then, rotation:

    Code:
    - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
    {
    	return TRUE;
    }
    
    -(void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration
    {
    	//[super willRotateToInterfaceOrientation:toInterfaceOrientation duration:duration];
    	return;
    	CGRect bgRect = bgView.bounds;
    
    	if (toInterfaceOrientation == UIInterfaceOrientationLandscapeRight || 
    		 toInterfaceOrientation == UIInterfaceOrientationLandscapeLeft)
    	{
    		//[bgView setBounds:CGRectMake(bgRect.origin.y, bgRect.origin.x, bgRect.size.height, bgRect.size.width)];
    		bgView.transform = CGAffineTransformMakeRotation(3.14159265/2);
    
    	}
    	
    	if (toInterfaceOrientation == UIInterfaceOrientationPortrait)
    	{
    		//[bgView setBounds:CGRectMake(bgRect.origin.y, bgRect.origin.x, bgRect.size.height, bgRect.size.width)];
    		bgView.transform = CGAffineTransformMakeRotation(3.14159265/2);
    	}
    }
    
    
    However, this transform has no effect. View rotates, but the image stays in vertical position, so the right half of the view has no bg.

    I've also tried to set new rect on the bgView as you can see form the commented code. Doesn't help, makes things worse. In landscape things are the same as w/o rect change, but when the view goes back to portrait bg image is moved down by 40-50 pix. No idea why.

    I've also tried to bring bgView to front, rotate and send back again, no effect either. I'm sortta out of ideas here :(

    So, what am I missing?
     
  2. PhoneyDeveloper macrumors 68030

    PhoneyDeveloper

    Joined:
    Sep 2, 2008
    #2
    I think you need to set the autoresize property for the bgview.
     
  3. Zvi thread starter macrumors newbie

    Joined:
    Sep 29, 2008
    #3
    I've added:
    Code:
    [bgView setAutoresizingMask:UIViewAutoresizingFlexibleHeight|UIViewAutoresizingFlexibleWidth];
    
    I've fixed it.
    I needed same flag on parent view too. After setting autoresize on main view all works fine.
     

Share This Page