Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Change UIWindow background color when device rotates

My UIWindow initially has a white background. I want to change the background to blue (permanently) when the device rotates. But what actually happens is the color briefly flashes blue then goes back to white.

In the app delegate:

- (void)application:(UIApplication *)application didChangeStatusBarOrientation:(UIInterfaceOrientation)oldStatusBarOrientation
{
    self.window.backgroundColor = [UIColor blueColor];
}

This code gets called as expected, but when the rotation finishes, -[UIWindow setBackgroundColor:] gets called a second time (as I discovered by subclassing UIWindow). The call stack for the second call is:

-[UIWindow setBackgroundColor]
-[UIWindow _finishedFullRotation:finished:context:]
-[UIViewAnimationState sendDelegateAnimationDidStop:finished:]
-[UIViewAnimationState animationDidStop:finished:]
-run_animation_callbacks
...

Edit (for @VinceBurn's answer)

The application has exactly one view (from Xcode's View-based Application project template). I have now set the view's background color (in IB) to 0% opacity. Still getting the same results.

To make sure the white color is not coming from some other default color setting, I am now initially setting the window's background color to green:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {    

    // Override point for customization after application launch.

    // Add the view controller's view to the window and display.
    self.window.backgroundColor = [UIColor greenColor];
    [self.window addSubview:viewController.view];
    [self.window makeKeyAndVisible];

    return YES;
}

When it rotates, it briefly flashes blue but returns to green.

like image 267
jlstrecker Avatar asked Dec 06 '11 17:12

jlstrecker


1 Answers

What I ended up doing was to change the background color of the active UIViewController's view, instead of the UIWindow:

- (void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation
{
    self.view.backgroundColor = [UIColor blueColor];
}

The reason I had been trying to set the background color of the UIWindow was to get the same background color throughout the app. Apparently the simplest way to accomplish that is to have all UIViewControllers in the app inherit from a UIViewController subclass that implements -didRotateFromInterfaceOrientation:.

like image 105
jlstrecker Avatar answered Nov 03 '22 14:11

jlstrecker