I need to change the image background of a View depending on the orientation. For this, I am using the statusBarOrientation approach in viewWillAppear
:
- (void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
UIInterfaceOrientation currentOrientation = [[UIApplication sharedApplication] statusBarOrientation];
if (UIInterfaceOrientationIsPortrait(currentOrientation)) {
NSLog(@"PORTRAIT");
} else if (UIInterfaceOrientationIsLandscape(currentOrientation)) {
NSLog(@"LANDSCAPE");
}
}
The problem is that the console is always showing PORTRAIT, even when the iPad is held in landscape mode. The same code in viewDidAppear
works correctly, but there is too late and the user can see the change of images. That makes me think that the correct state of statusBarOrientation is still not available in viewWillAppear
, but I have read in some other questions that this code should work there.
Try
int type = [[UIDevice currentDevice] orientation];
if (type == 1) {
NSLog(@"portrait default");
}else if(type ==2){
NSLog(@"portrait upside");
}else if(type ==3){
NSLog(@"Landscape right");
}else if(type ==4){
NSLog(@"Landscape left");
}
You shouldn't be using the statusBarOrientation to determine the current orientation of the application. According to Apple's doc: http://developer.apple.com/library/ios/#DOCUMENTATION/UIKit/Reference/UIApplication_Class/Reference/Reference.html
The value of this property is a constant that indicates an orientation of the receiver's status bar. See UIInterfaceOrientation for details. Setting this property rotates the status bar to the specified orientation without animating the transition. If your application has rotatable window content, however, you should not arbitrarily set status-bar orientation using this method. The status-bar orientation set by this method does not change if the device changes orientation.
Try using the interfaceOrientation property of a UIViewController to get the orientation of the current application.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With