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];
Related
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];
}
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]
I've set in plist that it should only support portrait mode, but when I rotate it the view changes. It worked for me before, but not now...
Please help, it's driving me crazy!
Vedran Burojević,
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
return (interfaceOrientation == UIInterfaceOrientationPortrait)
}
the above code only support portrait mode..
you can write the code in this.
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
return (interfaceOrientation == UIInterfaceOrientationPortrait)
}
Hi I have just started iPhone programming. I created a universal app for iPad and iPhone in Xcode 3.2 and evrerything is working fine. But now I want to implement following feature in that app:
It should support autorotation on iPad and be portrait only on iPhone.
I have no idea how to do that.
I have an AppDelegate_iPad delegate file,
an AppDelegate_iPhone delegate file,
and a viewcontroller under Shared tab and is share by both of these delegates.
Any ideas how can I implement that feature. any help would be appreciated.
Thanks
Vik
In the view controller(s) for the view(s) where you want to implement this rotation behavior, do the check inside the shouldAutorotateToInterfaceOrientation, like this:
- (BOOL)shouldAutorotateToInterfaceOrientation: (UIInterfaceOrientation) interfaceOrientation {
UIDevice* thisDevice = [UIDevice currentDevice];
if (thisDevice.userInterfaceIdiom == UIUserInterfaceIdiomPad)
{
return true;
}
else
{
return interfaceOrientation == UIDeviceOrientationPortrait;
}
}
Apps running iOS 6+ will also want to implement ViewController's:
- (NSInteger)supportedInterfaceOrientations
and/or AppDelegate's:
- (NSUInteger)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window
E.g.,
- (NSInteger)supportedInterfaceOrientations
{
if ([UIDevice currentDevice].userInterfaceIdiom == UIUserInterfaceIdiomPad) {
return UIInterfaceOrientationMaskAll;
}
return UIInterfaceOrientationMaskPortrait;
}
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