7

I have a background image in my main window so that when I flip views, it's not a blank white screen behind, but an image. My problem is that this image doesn't rotate when the device rotates.

Edit: As far as I can tell, Brett was correct when he pointed out that I'd have to rotate the background image manually in this instance. In case it helps anyone else out in the future, here's how I rotated it.

Inside myAppDelegate:

- (void) application:(UIApplication *)application
        willChangeStatusBarOrientation:(UIInterfaceOrientation)newStatusBarOrientation
        duration:(NSTimeInterval)duration
{
    if (newStatusBarOrientation == UIInterfaceOrientationPortrait)
        self.bgImage.transform      = CGAffineTransformIdentity;
    else if (newStatusBarOrientation == UIInterfaceOrientationPortraitUpsideDown)
        self.bgImage.transform      = CGAffineTransformMakeRotation(-M_PI);
    else if (UIInterfaceOrientationIsLandscape(newStatusBarOrientation))
    {
        float rotate    = ((newStatusBarOrientation == UIInterfaceOrientationLandscapeLeft) ? -1:1) * (M_PI / 2.0);
        self.bgImage.transform      = CGAffineTransformMakeRotation(rotate);
        self.bgImage.transform      = CGAffineTransformTranslate(self.bgImage.transform, 0, -self.bgImage.frame.origin.y);
    }
}
story
  • 729
  • 2
  • 9
  • 22
  • The UIImage has to be inside your UIView. Is it? – Peter Kazazes Aug 15 '11 at 19:02
  • 1
    Wow, really wanted to know how that worked (getting rid of blank screen when flipping). Upvote for you! – Jack Humphries Aug 15 '11 at 19:49
  • @Peter, So you're saying that a UIImageView won't autorotate on the main window, and I would have to put it inside of a UIView and declare a view controller for it? – story Aug 15 '11 at 21:00
  • You don't even have to create a new view controller for it. Just make sure it's *within* the UIView in IB. – Peter Kazazes Aug 15 '11 at 21:08
  • It's a tab bar template, so MainWindow.xib doesn't have a `UIView` other than `UIWindow`. This is where I placed my `UIImageView`. – story Aug 15 '11 at 21:16

2 Answers2

7

I think that UIWindow passes on rotation events to child controllers. The window should contain the root view controller view then pass on the rotation event message to the view controller to manage the rotation.

You should be able to listen for these events in your app delegate and manage the image rotation manually. Otherwise, just add the image as a subview of the root view controller.

Brett
  • 2,635
  • 22
  • 35
0

On your rootViewController make sure you implement this method:

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation {
return YES;
}

You rootViewController will probably just be a simple UIVIew with 2 UIViews, one for the background image view and the other for your main interface view.

puppybits
  • 1,110
  • 12
  • 16