Reputation: 3692
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 -
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
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