Bilbo Baggins
Bilbo Baggins

Reputation: 3692

CAGradientLayer - Doesn't cover the view completely in landscape orientation

I've used this tutorial to create a gradient background for my app. It looks beautiful. However there is a problem when I change the orientation.

It looks proper in portrait mode but in landscape orientation the gradient doesn't cover the entire view. I've uploaded a screenshot -

http://www.screencast.com/t/c4vUv55Xq0

The red is the gradient and the blue part is the default background color which is supposed to be completely covered by the red gradient.

How can I cover entire view? I tried to call the gradient method after detecting rotation change but it didn't work. This is the code I used:

    [[NSNotificationCenter defaultCenter]
     addObserver:self
     selector:@selector(deviceOrientationDidChangeNotification:)
     name:UIDeviceOrientationDidChangeNotification
     object:nil];// this is in 'viewWillAppear' method


- (void)deviceOrientationDidChangeNotification:(NSNotification*)note
{
    UIDeviceOrientation orientation = [[UIDevice currentDevice] orientation];
    [self addBackground];
}

Upvotes: 1

Views: 680

Answers (1)

rob mayoff
rob mayoff

Reputation: 385600

My guess is the system sends UIDeviceOrientationDidChangeNotification before actually updating the view hierarchy for the landscape layout.

Instead of redoing the gradient in response to UIDeviceOrientationDidChangeNotification, do it in viewDidLayoutSubviews. When your view controller receives viewDidLayoutSubviews, its view's frame has already been modified for the new interface orientation. Something like this should do:

- (void)viewDidLayoutSubviews {
    [super viewDidLayoutSubviews];
    self.background.frame = self.view.bounds;
}

Upvotes: 4

Related Questions