Ipad Simulator not auto rotating - iphone

I have an an iphone app and am using shouldAutorotateToInterfaceOrientation to determine when to autorotate. On the iphone, I specify that UIInterfaceOrientationPortrait is the only allowed orientation; On the iPad, I just return YES (ie all allowed), like so:
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) //also tried using [[UIDevice currentDevice] userInterfaceIdiom] to no avail
return YES; //doesn't get here
return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
This is in every view controller of the tab bar. When I NSLog() the [[UIDevice currentDevice] userInterfaceIdiom], it returns 0 (or UIUserInterfaceIdiomPhone).
Does the iPad simulator always return UIUserInterfaceIdiomPhone?

I think your answer is here: Does UI_USER_INTERFACE_IDIOM work with Targeted Device Family

Related

Rotation issue only on iPhone 3GS

I seem to be having a device specific problem with this code below, as it only affects the iPhone 3GS when I test.
Basically, the interface doesn't rotate. It works fine on all iPads and iPhones newer than the 3GS. If you can help me, I'd greatly appreciate it!
Code:
- (BOOL)shouldAutorotateToInterfaceOrientation: (UIInterfaceOrientation)interfaceOrientation {
if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) {
return YES;
} else {
return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown);
}
}
In your appDelegate didLaunch method, do you enable rotations:
[[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications];

Different Launch orientations for iPad and iPhone?

I'm making a universal app and I was wondering if it is possible to set initial orientation to Landscape for iPad and Portrait for iPhone? Currently, I'm setting initial interface orientation in the info.plist file but it doesn't seem to have different options for iPad and iPhone . If it cannot be done through info.plist file then how to do it programmatically?
programatically you can do using following code -
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPad) {
if (interfaceOrientation == UIInterfaceOrientationLandscapeLeft || interfaceOrientation == UIInterfaceOrientationLandscapeRight) {
return YES;
}
}
else {
if (interfaceOrientation == UIInterfaceOrientationPortrait) {
return YES;
}
}
return NO;
}
It looks like the initial interface orientation property conflicts with the supported orientations. I found the solution here, though.
UIDevice* thisDevice = [UIDevice currentDevice];
if(thisDevice.userInterfaceIdiom == UIUserInterfaceIdiomPad)
{
[[UIDevice currentDevice] setOrientation: UIInterfaceOrientationLandscapeRight];
}
else
{
[[UIDevice currentDevice] setOrientation:UIInterfaceOrientationPortrait];
}

iPad orientation not being returned correctly

I'm basically running this code:
UIInterfaceOrientation statusBarOrientation = [[UIApplication sharedApplication] statusBarOrientation];
if(statusBarOrientation == UIInterfaceOrientationPortrait){
NSLog(#"orientation is portrait");
}
However, regardless of the actual orientation in the simulator or my iPad, it is printing "orientation is portrait". Trying to NSLog the statusBarOrientation as a %d also returns 1 no matter what the orientation.
I've stuck this is my app delegate, my view controller, and the class that I need it in, and its the same thing. All 4 device orientations are supported in my info.plist / target settings.
Does anyone have a sure fire way of figuring out the interface orientation, or why mine is not working? Thanks
if you dont like to used Notification for orientation.. Then use below method too.
this is example of Only Landscape Orientation in iPad and Portrait in iPhone...
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
// Return YES for supported orientations
if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad)
{
if(interfaceOrientation==UIInterfaceOrientationLandscapeLeft ||
interfaceOrientation==UIInterfaceOrientationLandscapeLeft)
return YES;
else
return NO;
}
else
{
return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
}
You can REGISTER FOR notifications on orientation changes:
[[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(didRotate:)
name:#"UIDeviceOrientationDidChangeNotification"
object:nil];
- (void)didRotate:(NSNotification *)notification
{
UIDeviceOrientation orientation = [[UIDevice currentDevice] orientation];
if ((orientation == UIDeviceOrientationLandscapeLeft || orientation == UIDeviceOrientationLandscapeRight)) {
// DO STUFF
}
else if (orientation == UIDeviceOrientationPortrait) {
//DO MORE STUFF
}
}
Currently you are returning the orientation of the status bar. Instead get the orientation of the device: [[UIDevice currentDevice] orientation]

How to check the orientation of device programmatically in iPhone? [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
iPhone orientation
How to check the orientation of device at any point off time programmatically in iPhone ?
Thank You.
Here are macros UIDeviceOrientationIsLandscape and UIDeviceOrientationIsPortrait
so rather checking separately you can do it like this ...
if (UIDeviceOrientationIsLandscape([UIDevice currentDevice].orientation))
{
// code for landscape orientation
}
OR
if (UIDeviceOrientationIsPortrait([UIDevice currentDevice].orientation))
{
// code for Portrait orientation
}
Try it.
[[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications];
if ( ([[UIDevice currentDevice] orientation] == UIDeviceOrientationPortrait) )
{
// do something for Portrait mode
}
else if(([[UIDevice currentDevice] orientation] == UIDeviceOrientationLandscapeRight) || ([[UIDevice currentDevice] orientation] == UIInterfaceOrientationLandscapeLeft))
{
// do something for Landscape mode
}
Try also:
UIInterfaceOrientationIsPortrait(orientation)
and
UIInterfaceOrientationIsLandscape(orientation)
-[UIViewController interfaceOrientation]
However it is possible to retrieve the deviceOrientation, independent from the current interface orientation:
[[UIDevice currentDevice] orientation]
Read the documentation for more information though, you need to do some more things to make that work.
You can also check within the function
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
// Return YES for supported orientations
if(interfaceOrientation == UIInterfaceOrientationPortrait || interfaceOrientation == UIInterfaceOrientationPortraitUpsideDown)
{
//do for portrait
}
else
{
//do for Landscape
}
return (interfaceOrientation == UIInterfaceOrientationLandscapeRight ||
interfaceOrientation == UIInterfaceOrientationPortrait ||
interfaceOrientation == UIInterfaceOrientationLandscapeLeft ||
interfaceOrientation == UIInterfaceOrientationPortraitUpsideDown);
}
You should place [[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications]; in your AppDelegate didFinishLaunchingWithOptions
Then, anywhere in your application you can get the current orientation with:
UIDeviceOrientation orientation = [[UIDevice currentDevice] orientation];
And test orientation with:
UIInterfaceOrientationIsPortrait(orientation)
UIInterfaceOrientationIsLandscape(orientation)

How to update my UIViewController to current orientation when use pushViewController?

i have use this code but it is Private API ,apple also reject!
[[UIDevice currentDevice] setOrientation:UIInterfaceOrientationLandscapeRight];
if i rotate my device in Landscape then open my Application it will present as Portrait mode, i need to rotate my device to another orientation then it will update.
how to update it to correct orientation when call UIViewController?
You should use the follow code at your UIViewController class:
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
// Return YES for supported orientations
return (interfaceOrientation == UIInterfaceOrientationLandscapeRight);
}
if u need to update orientation at runtime then u have to return other constant
return (interfaceOrientation == UIInterfaceOrientationPortrait);