开发者

iPhone App - background disappears after memory warning

开发者 https://www.devze.com 2023-01-15 15:38 出处:网络
When I receive a memory warning in my navigation based iPhone App the background image disappears in the previously allocated navigation controllers.

When I receive a memory warning in my navigation based iPhone App the background image disappears in the previously allocated navigation controllers.

The background image is set as the background property of a UIView. This view is then added to the main window of the App delegate:

UIView *backgroundView = [[UIView alloc] initWithFrame: window.frame];

backgroundView.backgroundColor = [UIColor colorWithPatternImage:[[Utilities sharedManager] getImageWithName:@"Hintergrund2"]]; backgroundView.tag = 56789; [window addSubview:backgroundView]; [backgroundView release];

[window addSubview:[navigationController view]];

[window makeKeyAndVisible];

After I received the memory warning message and pressed the back button of the navigation controller the previous navigation controller shows up the normal Apple gray background. I have to navigate back to my home screen and navigate forward to "reset" the background ima开发者_如何学运维ge.

Can anyone tell me why the background disappears? It is not because of the image, when I set the background to a standard UIColor like blackColor the background (color) also disappears.

Thank you in advance for your help!

regards

Phil


It is undefined what the OS does with stuff added directly to the main UIWindow in case of low memory situations.

Maybe consider using a simple UIViewController for the background? You will then at least get callbacks when there is a low memory situation.


First, you should probably add your controller to the window like this:

window.rootViewController = navigationController;

I haven't tested this, but perhaps you can set the background color of the window directly:

window.backgroundColor = [UIColor colorWithPatternImage:[[Utilities sharedManager] getImageWithName:@"Hintergrund2"]];

If that doesn't work, you'll need a better way to recreate your view state in low memory situations. The most standard way to do this is by defining the background in each xib, which is reloaded. Alternatively, you could catch the low memory warning and recreate as needed by seeing if your backgroundView has a superview.


Make sure that you are setting the calling self.view.backgroundColor = [UIColor clearColor]; in the view controller's - (void) viewDidLoad otherwise the window's image will not be displayed.

- (void) viewDidLoad
{
    [super viewDidLoad];

    self.view.backgroundColor = [UIColor clearColor];
...
}

My problem was that I set the view's backgroundColor when it is initialized

MyTableViewController  *myTVC =
    [[MyTableViewController alloc] initWithStyle:UITableViewStyleGrouped];
myTVC.view.backgroundColor = [UIColor clearColor];

and not in - (void) viewDidLoad.

In this situation, the UITableViewController's view's background color was set after it was initially created. However after a low memory warning, the UIViewController's that were not displayed have - (void) viewDidUnload called on them. That deallocate their views. Just before the UIViewController was to be displayed again, - (void) viewDidLoad was called and created a new view, but the view's background color was set to the default which is not clear.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号