UIInterfaceOrientation on launch - iphone

I'm not sure if this a bug or something I might do wrong, but when I launch my application in portrait mode without rotating the device and I run this code
if(UIInterfaceOrientationIsPortrait([[UIDevice currentDevice] orientation])) {
NSLog(#"Portrait");
} else {
NSLog(#"Landscape");
}
I return Landscape, after I rotate the device and check again it returns the correct value, so it seems that when you first launch the returned orientation is wrong.
Is this a known issue?
EDIT:
Even when I run this code in my AppDelegate it's returning Landscape, even when I launch in Portrait mode
if(UIInterfaceOrientationIsPortrait([[UIDevice currentDevice] orientation])) {
NSLog(#"Portrait");
} else {
NSLog(#"Landscape");
}

That's because at first launch it returns UIDeviceOrientationUnknown. It's neither portrait nor landscape. If you want to detect the user interface orientation, you should use
UIInterfaceOrientation orientation = [UIApplication sharedApplication].statusBarOrientation;
if (UIInterfaceOrientationIsLandscape(orientation)) {
NSLog(#"landscape");
}else{
NSLog(#"portrait");
}

In your Info.plist > Supported interface orientations drag the item "Portrait (bottom home button)" up over the other items.

this is the code to Portrait Mode Only
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return UIInterfaceOrientationIsPortrait(interfaceOrientation);
}

Related

xcode - How do I keep views locked into portrait mode, but still allow one view to rotate?

I'm having a problem with device rotation. Except for ONE view, where I show a company splash screen, I want to lock all the remaining app views into Portrait display. In the project settings the supported orientations are Portrait and LandscapeLeft. In the 'Company Splash' it works fine and the view rotation is locked into LandscapeLeft no matter how I rotate the device. On all the other views when I rotate the device to the left the view alters instead of staying in portrait display. The methods are not even firing? If I remove Landscape left from the supported Orientations in the project, that screws up the 'Company Splash' view. I tried changing the shouldAutorotate return to NO, but that didn't help. Tried to work my way through the suggestions posted here, but that didn't help. If I put the following code into my AppDelegate.m, everything is locked into portrait mode and the 'Company Splash' crashes when accessed.
-(NSUInteger)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window
{
return UIInterfaceOrientationMaskPortrait | UIInterfaceOrientationMaskPortraitUpsideDown;
}
How do I lock the view into portrait mode no matter how the device is rotated except for the one screen?
** method from the 'Company Splash' view. Again, works like it is supposed to.
-(NSInteger)supportedInterfaceOrientations
{
return UIInterfaceOrientationMaskLandscapeLeft;
}
** methods from all the other views, which rotate out of portrait when I don't want them to
-(BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
// IOS5 Only returning that it should rotate to potrait
return (interfaceOrientation == UIDeviceOrientationPortrait);
}
-(BOOL)shouldAutorotate
{
// forcing the rotate IOS6 Only
return YES;
}
-(NSInteger)supportedInterfaceOrientations
{
// return number or enum IOS6 Only
return UIInterfaceOrientationMaskPortrait;
}
I thought maybe it might be because a UITabBarController is the root controller and I'm in a ViewController off of that? The methods are not even firing?
Add an observer to the viewDidLoad method of the view you want to rotate like this :
[[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications];
[[NSNotificationCenter defaultCenter]
addObserver:self selector:#selector(orientationChanged:)
name:UIDeviceOrientationDidChangeNotification
object:[UIDevice currentDevice]];
and then set the views according the the landscape view inside the orientationChanged method like this :
- (void) orientationChanged:(NSNotification *)note{
UIDevice * device = [UIDevice currentDevice];
switch(device.orientation)
{
case UIDeviceOrientationPortrait:
break;
case UIDeviceOrientationPortraitUpsideDown:
break;
case UIDeviceOrientationLandscapeLeft:
break;
case UIDeviceOrientationLandscapeRight:
break;
default:
break;
};
}
Slap this method into the view controller you want locked in a certain orientation.
-(NSUInteger)supportedInterfaceOrientations
{
return UIInterfaceOrientationMaskLandscape;
}
just change to the orientation you want (the above locks it in landscape)
maybe u should allow all orientations, and lock portrait orientation in each class except company splash with
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
if (interfaceOrientation == UIInterfaceOrientationPortrait) {
return (interfaceOrientation == UIInterfaceOrientationLandscapeRight);
} else {
return (interfaceOrientation == UIInterfaceOrientationLandscapeRight);
}
}

convert iphone to ipad but screen do not auto rotate while rotate the ipad

I convert my tabbar based iphone app to ipad version with different ~ipad.xib, the only different is the ui element size. Now my question is that it can't auto rotate the screen in ipad version, i.e. I upsidedown the ipad, but the ui don't rotate upsidedown.
You need to return YES for the shouldAutorotateToInterfaceOrientation: for all view controllers featured in your tab bar, for iPad only (UI_INTERFACE_IDIOM()...). The default code from your iPhone controllers probably only returns YES if it is portrait, if that is all you selected when initially creating the project.
Here is the code I use for this situation (iPhone, portrait only, iPad, all orientations):
-(BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation
{
if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad)
return YES;
else
return toInterfaceOrientation == UIInterfaceOrientationPortrait;
}
Tab bar controller can not auto rotate. you have to rotate all the component.
You can use this code to accomplish your task in case of universal apps
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
// Return YES for supported orientations
BOOL ret;
if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad){
// iPad-specific code
if (interfaceOrientation != UIInterfaceOrientationPortrait) {
ret = YES;
}else{
ret = NO;
}
} else {
// iPhone-specific code
ret = YES;
}
return ret;
}
You need to modify 'Supported Device Orientations' to include your new orientations

Programmatically change device orientation in iPhone

I have navigation based app, when I click on any row in root view, the next view should come in Landscape mode.
I am not able to find a proper way to implement this. What I tried is:
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
return UIInterfaceOrientationIsLandscape(interfaceOrientation);
}
But this is not working. The view should open only in Landscape mode (not when user rotate it).
My sandbox app:
https://github.com/comonitos/programatical_device_orientation
The solution is easy:
in interface (h file) :
BOOL rotated;
in implementation (m file):
1. rewrite
-(BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return rotated;
}
2 call [self setup]
-(void) setup {
rotated = YES;
[[UIDevice currentDevice] setOrientation:UIDeviceOrientationLandscapeLeft];
rotated = NO;
}
Try designing your view in landscape mode and in it's view controller, and in
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
.....
}
return No for portrait orientation and YES for landscape orientation
This will display your view in landscape mode.
you can force your Device to change Orientation.......using magic line in View will Appear
[[UIDevice currentDevice] setOrientation:UIDeviceOrientationLandscapeRight];
All the Best..
Here is some code from my landscape-view based app:
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation{
if (interfaceOrientation == UIInterfaceOrientationLandscapeRight || interfaceOrientation == UIInterfaceOrientationLandscapeLeft){
return YES;
}
else {
return NO;
}
}
Put this in a landscape-based viewcontroller to have it display only in landscape mode and to switch between landscape left and landscape right. You will also need your xib to be landscape based.

UIInterfaceOrientation bug?

In my iPhone app I need to detect the current orientation and I have to determine if I'm in portrait or landscape. I use this code:
UIInterfaceOrientation orientation = [[UIDevice currentDevice] orientation];
if (orientation == UIInterfaceOrientationPortrait || orientation == UIInterfaceOrientationPortraitUpsideDown) {
NSLog(#"portrait");
...
} else {
NSLog(#"landscape");
...
}
Everything is ok when my iPhone is in my hand.
But when i put it on the table and i run the application, the content is displayed on the screen in portrait mode and my code goes to else and NSLog prints landscape.
Is my test incomplete ? How to prevent this case ?
EDIT : the test is performed in my controller viewDidLoad method and my application handle rotation.
UIDevice.orientation is of type UIDeviceOrientation, which is a superset of UIInterfaceOrientation. You are probably getting the value UIDeviceOrientationFaceUp.
This underscores that yes, your test is incomplete. You should write something like this:
UIInterfaceOrientation orientation = [[UIDevice currentDevice] orientation];
if (orientation == UIInterfaceOrientationPortrait || orientation == UIInterfaceOrientationPortraitUpsideDown) {
NSLog(#"portrait");
...
} else if (orientation == UIInterfaceOrientationLandscapeLeft || orientation == UIInterfaceOrientationLandscapeRight) {
NSLog(#"landscape");
...
} else {
NSLog(#"WTF? %d", orientation);
assert(false);
}
Then, you'll know if you if you've missed something.
UIDevice.orientation can return that the device is flat or upside down (not inverted portrait, upside-down as in laying on its face). Instead call UIViewController.interfaceOrientation on your root view controller.
I would recommend using UIDeviceOrientationIsValidInterfaceOrientation(orientation)
It will tell you if its a valid orientation (valid being either landscape or portrait, not FaceUp/FaceDown/UnKnown). Then you can treat it as if its portrait if its unknown.
This is how I do it:
if (UIDeviceOrientationIsValidInterfaceOrientation(interfaceOrientation) && UIInterfaceOrientationIsLandscape(interfaceOrientation)) {
// handle landscape
} else {
// handle portrait
}

How can I query the current orientation of an iPhone/iPad screen?

I was curious to know if there is some way to determine the orientation of a screen on iPhone/iPad.
Currently I find myself setting a member variable when this message is called:
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
self.orientation = interfaceOrientation;
return YES;
}
... which feels like something I should be able to query as a state instead of having to track it when it changes.
UIViewController has the interfaceOrientation property.
Don't get confused by orientation in UIDevice, which is not the orientation of the interface, but the physical orientation.
Use this if you have a status bar on top.
UIInterfaceOrientation orientation = [[UIApplication sharedApplication] statusBarOrientation];
if (orientation == UIInterfaceOrientationPortrait || orientation == UIInterfaceOrientationPortraitUpsideDown) {
// portrait
} else {
// landscape
}
You can do [[UIDevice currentDevice] orientation].