UI autorotation works only on some devices? - iphone

I'm having quite interesting (but very annoying) problem with (not only) UI autorotation. I'm working on the app which contains tabbar controller with couple of navigation controllers as tab items. One of those controllers allows for autorotation to landscape mode.
Interesting is that it perfectly works in Simulator as well as on my own device, but it's not working on other testing devices. All devices are the same - iPhone 3G 16 GB, OS version 3.1.3.
When I'm looking for a log messages which report orientation change events in console, I can find it again only on my device, but not on other devices, so it seems that other devices do not report orientation change at all. Which is very unlikely on the other hand, because other apps are working normally... Also some other features which are working on my phone are not working on other devices too (touch events handling in tableview for instance).
I've never seen something like this and cannot find any reason why the hell it should not work on all devices the same way! So the question is - am I mad or what?
I use subclass of UITabBarViewController with overriden method
- (BOOL) shouldAutorotateToInterfaceOrientation: (UIInterfaceOrientation) toInterfaceOrientation {
BOOL shouldRotate = (toInterfaceOrientation == UIInterfaceOrientationPortrait || toInterfaceOrientation == UIInterfaceOrientationLandscapeLeft);
NSLog(#"Should rotate: %d", shouldRotate);
return shouldRotate;
}
Then I handle orientation change animation in respective methods (willRotate..., willAnimateRotation...). But as I said - it works fine on my device, but on other devices the shouldAutorotateToInterfaceOrientation: method gets never called. Weird!
Please give me some hint before I'll end in a madhouse. Thanks for any tip.

Related

iPhone app autorotates on one device, but not another

Does anyone know of a reason why an iPhone would autorotate on one device, but not another? I can't reproduce it on my iPhone, iPod Touch, or any of the simulator devices.
Are there any device settings I should be paying attention to? The offending iPhone has the same SDK version installed (4.2.1), but is a different model (MC319LL) than mine (MC605DN).
Well, I never figured out why the two phones were acting differently, but this bit of code in in the UITabBarController subclass seems to have solved things:
- (BOOL) shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation
{
return toInterfaceOrientation == UIInterfaceOrientationPortrait;
}
You made sure that the misbehaving device had its rotation lock turned off in the settings, right?

How to restrict my app to landscape mode?

I have my iPad application created using the SplitView template.
I wonder what is the best way to restrict my application to landscape mode?
I have tried overriding shouldAutorotateToInterfaceOrientation: method in DetailViewController.m
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
return UIInterfaceOrientationIsLandscape(interfaceOrientation);
}
but 4.2 GM is still buggy and it fails to show the controller view. What other choices do I have?
Thanks in advance.
UPDATE1
I have already filed a bug report:
Bug ID #8620135
My app is almost finished and I have to find a work-arround since I don't think they are going to solve this before 4.2 officially comes out (GM is already out!)
In order to recreate the bug, just use SplitView template and override above method in any of the UIViewControllers (RootViewController or DetailViewControllers)
UPDATE2
I have found a work-around. (See UPDATE3 for the complete work-around)
Set UISupportedInterfaceOrientations only to support Landscape , this will force the app to start in landscape mode allowing DetailViewController to start correctly(hence shown correctly)
<key>UISupportedInterfaceOrientations</key>
<array>
<string>UIInterfaceOrientationLandscapeLeft</string>
<string>UIInterfaceOrientationLandscapeRight</string>
</array>
But if you rotate the device, it turns Portrait mode!!!, so is still necessary to override shouldAutorotateToIntercafeOrientation: as above
Discussion:
If this wouldn't be a bug I would expect a warning or execution error, exception or something when starting the app in a orientation that is not supported by the view controller. Besides, why only DetailViewController does not show? If this would be specification, then RootViewController should also fail to load then. Don't you think?
thanks for you help... ;)
UPDATE3
After further tests I have realized that above work-around does not work in some cases. For example when starting the app when the device is in landscape won't work!.
The real problem seems to be that in iOS4.2GM UISplitViewController needs all its controllers have all rotations to be available at its load time. So is necessary to trick him so it loads in Landscape mode and then not allow him to rotate its view controllers.
So here is the new work-around for this annoying iBug.
Step1:
Set Info.plist like so:
<key>UISupportedInterfaceOrientations</key>
<array>
<string>UIInterfaceOrientationLandscapeLeft</string>
<string>UIInterfaceOrientationLandscapeRight</string>
</array>
Step2
Set a new flag in DetailViewController.m or .h (from SplitView Template)
BOOL lockRotation = NO; //WORK-ARROUND: Bug ID# 8620135.
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
//WORK-ARROUND: Bug ID# 8620135.
if (lockRotation) {
return UIInterfaceOrientationIsLandscape(interfaceOrientation);
}else{
return YES;
}
}
- (void)viewDidLoad {
[super viewDidLoad];
//set NO here since this is called before shouldAutorotateToInterfaceOrientation method is called
lockRotation = NO; //WORK-ARROUND: Bug ID# 8620135.
}
- (void)viewDidAppear:(BOOL)animated {
//set YES as soon as possible, but after shouldAutorotateToInterfaceOrientation method was called
lockRotation = YES; //WORK-ARROUND: Bug ID# 8620135.
[super viewDidAppear:animated];
}
IMPORTANT NOTE:
Please note that this bug only appears when the UISplitViewController is loaded and not everytime
the its view appears. Hence, to see this bug make sure the app was terminated before.
I asked a question with a bounty of 500 that seems to be the same thing you're facing.
From my limited experience it is much easier to make a landscape-only iPhone app than a landscape-only iPad app. I'm not sure why there is any difference, but the steps Apple says to take to make it landscape-only do not work on their own.
Try this (it works):
-(BOOL)shouldAutorotateToInterfaceOrientation(UIInterfaceOrientation)toInterfaceOrientation {
if(toInterfaceOrientation == UIInterfaceOrientationLandscapeRight)
return YES;
}
else
{
return NO;
}
}
Check out this iPhone app in landscape mode, if you haven't already. I was going to suggest simply adding UISupportedInterfaceOrientations to your Info.plist and specifying the two landscape orientations. But, apparently, this is not sufficient, according to answers to cited question.
I believe this is a BUG, I faced this problem also. It is something to do with
UIInterfaceOrientationLandscapeLeft
To replicate this situation:
1) Create a new iPad project using UISplitViewController template
2) Edit info.plist
Supported interface orientations
-Landscape (left home button)
-Landscape (right home button)
3) DetailViewController.m
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
// return YES;
NSLog(#"RotateToInterface:[%d] vs LandscapeLeft[%d]", interfaceOrientation, UIInterfaceOrientationLandscapeLeft);
return interfaceOrientation == UIInterfaceOrientationLandscapeLeft;
}
4) Run it....You will see a blank black view. and no matter how you turn. "UIInterfaceOrientationLandscapeLeft" never detected.
By the way, nacho4d's adding BOOL check work-around is working. Thumbs UP :)

iPhone locked Portrait, iPad locked Landscape

I'm trying to convert an iPhone app to iPad. The tricky things is that the iPhone app has to be locked to portrait view and the iPad app has to be locked to landscape view. I'm kind-of a noob at interface builder so I'm a little lost.
Any help is greatly appreciated. Thanks!
You need to override shouldAutorotateToInterfaceOrientation. A good place to put this is in the app delegate.
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad &&
UIInterfaceOrientationIsLandscape(interfaceOrientation))
{
return YES;
}
if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone &&
UIInterfaceOrientationIsPortrait(interfaceOrientation))
{
return YES;
}
return NO;
}
I'm not sure, but this may also support the phone being upside-down, which is a HIG no no. You might want to use interfaceOrientation == UIInterfaceOrientationPortrait instead for the phone.
You should have two nibs and load them separately depending on which device your application determines it is running on: one for the iPad and one for the iPhone. You can then set the orientation easily. NB: The iPad app should support all orientations variants (ie. if you support portrait, support portrait upside-down) and will most likely get rejected by Apple unless you have a compelling reason as to why it shouldn't.

Launching application in landscape orientation for IPad

Facing one issue with launching application in landscape orientation for IPad.
I have developed IPhone application which later I ported to IPad.
I have made setting regarding orientation in info.plist
[ UISupportedInterfaceOrientations~ipad ] to support all orientation UIInterfaceOrientationPortrait , UIInterfaceOrientationPortraitUpsideDown , UIInterfaceOrientationLandscapeLeft , UIInterfaceOrientationLandscapeRight.
but when I start IPad application in the landscape mode, it always start in the potrait mode.
Along this
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{ return YES; }
help me, if I am missing something with this..
Thanks,
Sagar
here's something I also discovered: setting the initial interface orientation in your info.plist is being ignored if you have Supported interface orientations set up with another orientation in the first slot! Put your initial orientation there as well - and the simulator will launch correctly, as will the app. this drove me nuts for a long time!
Put UISupportedInterfaceOrientations into your -Info.plist, with a setting for each orientation you support. This is used to see which orientation the app can start in. From there onwards it will ask your view controllers.
Sagar - I had the same issue but was able to resolve it.
Like yours, my app started as an iPhone app which I "upgraded" to a Universal app using the XCode wizard. I noticed that when running on the actual iPad, starting in landscape, the app would start in Portrait, then maybe rotate to Landscape. On the simulator, starting in landscape, the app would start in Landscape, then the simulator would rotate to Portrait.
On the iPad, my app is a split-view app with TabBarControllers on left and right. Each tab is a view controller that returns YES to shouldAutoRotateToInterfaceOrientation.
I noticed that a brand-new wizard-generated, simple-case with a splitviewcontroller, Universal app didn't have this problem.
The difference I found between my app and the simple-case was that I wasn't adding my splitview-controller's view to the app window in applicationDidFinishLaunchingWithOptions. Instead I was showing a "loading" view at this stage, then later when an initialization thread completed I'd add my splitviewcontroller's view (and hide the "loading" view).
When I added my splitviewcontroller's view to the app window during the call to applicationDidFinishLaunchingWithOptions everything started working fine.
There must be some magic that happens on return from applicationDidFinishLaunchingWithOptions???
Is your app similar to mine in that it isn't adding the main view controller's view to the window during applicationDidFinishLaunchingWithOptions?
As pointed out in a number of posts, you must set up the info.plist with both the supported and the initial interface orientations. However, the bigger issue is when does the initial orientation become effective? The answer is NOT when your view controller receives the "viewDidLoad" message. I found that on the iPad-1, running iOS 5.0, the requested initial orientation becomes effective only after several "shouldAutorotateToInterfaceOrientation"
messages are received.(This message passes the UIInterfaceOrientation parameter to the receiver.) Furthermore, even if the orientation says it is in Landscape mode, it may not be! The only way I found to be sure that the view is in Landscape mode is to test that the view height is less than the view width.
The strategy that worked for me was to lay out the subViews I wanted when the "viewDidLoad" message was received but to delay actually adding those subViews to the view until the controller received a valid "shouldAutorotate.." message with the orientation set to Landscape mode. The code looks something like:
(BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
// Return YES for supported orientations
// N.B. Even when the interface orientation indicates landscape mode
// this may not really be true. So we insure this is so by testing
// that the height of the view is less than the width
if (interfaceOrientation == UIInterfaceOrientationLandscapeLeft ||
interfaceOrientation == UIInterfaceOrientationLandscapeRight)
{
CGRect viewBounds = [[self view] bounds];
if ( viewBounds.size.height < viewBounds.size.width )
[self addMySubViews];
return YES;
}
else
return NO;
}
Apple has just released iOS 5.1, so this behavior may have changed. But I expect the code that is here should still work.

iPad launch orientation not detected

I have an iPad app that works correctly except for an odd issue during launch. I've read several questions & answers regarding orientation, but this still has me stumped.
The root view controller is a UITabBarController with 3 tabs. Two of the tabs are have custom view controllers (one based off of UIViewController, the other off of UITableViewController) and both suffer from this launch orientation problem. The third tab is a custom UITableViewController that's embedded in a UINavigationController.
OK, here's the problem. If I start the app in Portrait orientation, everything works great. If I start it in Landscape orientation, the 3rd tab works perfectly. However, the first 2 tabs come up in Portrait orientation, even though:
The status bar orientation correctly shows as landscape (spread across the screen).
The Tab Bar view correctly shows as landscape with the tabs centered.
All views return YES for shouldAutorotateToInterfaceOrientation for all orientations.
If I call [self interfaceOrientation] or [[UIApplication sharedApplication] statusBarOrientation] in the view controller's viewWillAppear, then the 3rd tab's view controller reports 3 (landscape) but the first two view controllers report 1 (portrait) even though the status bar is clearly landscape!
If I rotate the iPad to portrait and back to landscape, then all 3 tabs' views rotate correctly (and the methods above return 3, as expected).
Also, if I tap on any other tab and then back on tab #1 or #2, then they will now rotate correctly, even without rotating the iPad itself!
What am I missing?
You have to add the supportedDeviceOrientations to your "myApp.plist" .
Click on this list, add the key "Supported interface orientations" and add the supported interface orientations. This solved the problem for me.
For further informationen follow this link and go to the section "The Application Bundle": http://developer.apple.com/iphone/library/documentation/General/Conceptual/iPadProgrammingGuide/CoreApplication/CoreApplication.html
I finally found my answer: I just forgot this in my LoadingController.
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
return YES;
}
I have found that the device orientation starts out with nothing. And should return YES for Unknown. This will allow it to orient the device with the correct launch orientation.
Here is the code I used to propigate this message up to the legacy messages.
- (BOOL)shouldAutorotate{
UIDeviceOrientation orientation = [UIDevice currentDevice].orientation;
if (orientation == UIDeviceOrientationUnknown) return YES;
BOOL result = [self shouldAutorotateToInterfaceOrientation:orientation];
return result;
}
notice I return YES if orientation == UIDeviceOrientationUnknown. This corrected my loading problem.
The solution is to add a key
UISupportedInterfaceOrientation
to you Info.plist with an array of strings specifying the suppored interface orientations at launch time, these are
UIInterfaceOrientationPortrait
UIInterfaceOrientationPortraitUpsideDown
UIInterfaceOrientationLandscapeLeft
UIInterfaceOrientationLandscapeRight
However, there is the follwing issue which may lead to confusion: At least with SDK 3.2 and iPad Simulator from XCode 3.2.4 I found that (at least some) Info.plist settings appeared to be cached and/or are not updated when installing the app. That is, adding the key above and installing and launching the app in the simulator had no effect. However, deleting the app from the simulator fixed the problem an the newly installed app behaved as specified.
In your app delegate's applicationDidFinishLaunchingWithOptions: method, after you add your view controller's view to the window, add the following:
[myViewController viewDidLoad];
If necessary, this will trigger a call to the shouldAutorotateToInterfaceOrientation: method.
just try this
- (BOOL)shouldAutorotateToInterfaceOrientation: UIInterfaceOrientation)interfaceOrientation {
return (interfaceOrientation==UIInterfaceOrientationPortraitUpsideDown);<br>
}