Advertise Mobile SDKs Books Events Forum News Social Networking Support Us
Follow @iphonedevsdk on Twitter

Interface 2, Advanced iOS
Mockup & Code Gen
($9.99)

Make your own iPhone apps
and run them live!
(free)

Pic Frame Dynamo: Photo Editing
($0.99)

Abiliator
($1.99)

Want your application or service advertised on iPhone Dev SDK?

Go Back   iPhone Dev SDK Forum > iPhone SDK Development Forums > iPhone SDK Development

Reply
 
LinkBack Thread Tools Display Modes
Old 04-07-2010, 12:33 PM   #1 (permalink)
Emphasizing Fundamentals
 
BrianSlick's Avatar
 
Join Date: Jul 2009
Location: NoVA / DC Area
Age: 36
Posts: 7,990
BrianSlick has a spectacular aura about
Default How To Determine iPad Launch Orientation?

I've got an iPhone app with a little animation at startup. The desired effect is a fade/zoom of the Default.png. How I accomplish this is adding an image view in the app delegate that has the same image, then I do a UIViewAnimation block on it. Works just fine.

Enter iPad, and the conversion process to make this a universal app.

I know about Default-Landscape.png and Default-Portrait.png and those work just fine. But since it could be either image, I need to know which one to put in my image view for the animation.

The problem I'm encountering is that everything I know to ask is reporting portrait, even if the device/simulator launches landscape. Checking the frame of the UIWindow gives portrait dimensions, as does [UIScreen mainScreen]. Asking UIDevice for orientation reports portrait. Since this is the app delegate, I don't have a view controller yet to ask for orientation.

I found a little nugget on the Apple dev forums that says that view controllers are always created portrait, and then rotated without animation if necessary before being put on the screen. So I change my approach and use a view controller instead of just the image view.

This always goes portrait:
Code:
- (void)viewWillAppear:(BOOL)animated
{
	NSLog(@">>> Entering %s <<<", __PRETTY_FUNCTION__);
	
	[super viewWillAppear:animated];
	
	if ( ([self interfaceOrientation] == UIInterfaceOrientationLandscapeLeft) || ([self interfaceOrientation] == UIInterfaceOrientationLandscapeRight) )
	{
		NSLog(@"Using landscape image");
		[[self mainImageView] setImage:[UIImage imageNamed:@"Default-Landscape.png"]];
	}
	else
	{
		NSLog(@"Using portrait image");
		[[self mainImageView] setImage:[UIImage imageNamed:@"Default-Portrait.png"]];
	}
	
	NSLog(@"<<< Leaving %s >>>", __PRETTY_FUNCTION__);
}
I got this to work, but it seems a tad unnecessary. And it only works on the device, it does not work in the simulator.
Code:
- (void)viewWillAppear:(BOOL)animated
{
	NSLog(@">>> Entering %s <<<", __PRETTY_FUNCTION__);
	
	[super viewWillAppear:animated];
	
	[[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications];
	
	if ( ([[UIDevice currentDevice] orientation] == UIDeviceOrientationLandscapeLeft) || ([[UIDevice currentDevice] orientation] == UIDeviceOrientationLandscapeRight) )
	{
		NSLog(@"Using landscape image");
		[[self mainImageView] setImage:[UIImage imageNamed:@"Default-Landscape.png"]];
	}
	else
	{
		NSLog(@"Using portrait image");
		[[self mainImageView] setImage:[UIImage imageNamed:@"Default-Portrait.png"]];
	}
	
	[[UIDevice currentDevice] endGeneratingDeviceOrientationNotifications];
	
	NSLog(@"<<< Leaving %s >>>", __PRETTY_FUNCTION__);
}
I feel like I'm missing something obvious, but nothing else I've tried is working. Surely there has to be something easier, and something that would work on the simulator too. Any tips?
__________________
BriTer Ideas LLC - Professional iOS App Development. Available for hire.

SlickShopper 2 | Free NSLog utility | Leave a PayPal donation.

Are you a newbie? Things you should read:
Definitive Guide To Properties | UITableView Series | Guide To Troubleshooting | Model Object Overview

Do you sit at a desk all day? Walk instead! Follow along with my treadmill desk adventures.
BrianSlick is offline   Reply With Quote
Old 04-07-2010, 02:18 PM   #2 (permalink)
Registered Member
 
Join Date: Nov 2009
Posts: 580
ChrisL is on a distinguished road
Default

It's not obvious, but but this
Code:
[[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications];
UIInterfaceOrientation orientation = [UIDevice currentDevice].orientation;
works correctly for me on the device (haven't tried it in the simulator, though). I got stuck on this, too, when trying to ask UIDevice for the orientation inside didFinishLaunching, because I didn't realize at first that I had to explicitly turn on orientation notifications.

According to the UIDevice reference:
Quote:
The value of this property always returns 0 unless orientation notifications have been enabled by calling beginGeneratingDeviceOrientationNotifications.
I had initially assumed that this property contained the current orientation at all times, but not so, apparently. I guess that turning on notifications is being handled for us behind the scenes in other situations where the orientation property is typically accessed, so it wasn't obvious that this needs to be done manually inside the app delegate.
ChrisL is offline   Reply With Quote
Old 04-07-2010, 03:41 PM   #3 (permalink)
Emphasizing Fundamentals
 
BrianSlick's Avatar
 
Join Date: Jul 2009
Location: NoVA / DC Area
Age: 36
Posts: 7,990
BrianSlick has a spectacular aura about
Default

Alright, I'll stick with what is working, then. Too bad there doesn't seem to be a simulator solution, though.
__________________
BriTer Ideas LLC - Professional iOS App Development. Available for hire.

SlickShopper 2 | Free NSLog utility | Leave a PayPal donation.

Are you a newbie? Things you should read:
Definitive Guide To Properties | UITableView Series | Guide To Troubleshooting | Model Object Overview

Do you sit at a desk all day? Walk instead! Follow along with my treadmill desk adventures.
BrianSlick is offline   Reply With Quote
Old 05-05-2010, 05:20 PM   #4 (permalink)
see my iOS apps! :D
 
Join Date: Sep 2008
Location: Europe
Posts: 296
LunarMoon is on a distinguished road
Default

Quote:
Originally Posted by BrianSlick View Post
Alright, I'll stick with what is working, then. Too bad there doesn't seem to be a simulator solution, though.
Hi, have you figured out a solution for the simulator? thanks!
LunarMoon is offline   Reply With Quote
Old 05-05-2010, 07:39 PM   #5 (permalink)
Emphasizing Fundamentals
 
BrianSlick's Avatar
 
Join Date: Jul 2009
Location: NoVA / DC Area
Age: 36
Posts: 7,990
BrianSlick has a spectacular aura about
Default

Haven't looked any further. Not worth my time.
__________________
BriTer Ideas LLC - Professional iOS App Development. Available for hire.

SlickShopper 2 | Free NSLog utility | Leave a PayPal donation.

Are you a newbie? Things you should read:
Definitive Guide To Properties | UITableView Series | Guide To Troubleshooting | Model Object Overview

Do you sit at a desk all day? Walk instead! Follow along with my treadmill desk adventures.
BrianSlick is offline   Reply With Quote
Old 05-09-2011, 06:34 PM   #6 (permalink)
Registered Member
 
Join Date: May 2011
Posts: 2
JEllis is on a distinguished road
Default

I believe this works.


if (([[UIApplication sharedApplication] statusBarOrientation] == UIInterfaceOrientationLandscapeLeft) ||
([[UIApplication sharedApplication] statusBarOrientation] == UIInterfaceOrientationLandscapeRight)) {
set up your landscape view here
} else {
set up your portrait view here

I have a tab bar controller and 4 view controllers, and use this in each view controller at ViewDidLoad, ViewDidAppear, when it comes back from background processing, and after returning from reviewing an iAd. Since it follows the status bar it seems to always be in sync with it. Haven't checked to see if it works in the simulator.
JEllis is offline   Reply With Quote
Old 12-10-2011, 05:01 AM   #7 (permalink)
Nuisance Developer
 
Join Date: Jul 2009
Location: Italy
Posts: 4,691
dany_dev is on a distinguished road
Default

I had the same problem,

Basically I solved writing

Code:
- (void)viewWillAppear:(BOOL)animated
{

   [self repositionUIWithInterfaceOrientation:self.interfaceOrientation];

   [super viewWillAppear:animated];

}

- (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration {

   [self repositionUIWithInterfaceOrientation:toInterfaceOrientation];
	
}
So when i launch the app on the iPad (that is in landscape), viewWillAppear say "portrait" but after that is called willRotateToInterfaceOrientation that say "landscape", and when i launch the app with the iPad (that is in portrait), viewWillAppear say "portrait" and willRotateToInterfaceOrientation is not called.
__________________

Last edited by dany_dev; 12-10-2011 at 05:04 AM.
dany_dev is offline   Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



» Advertisements
» Online Users: 320
14 members and 306 guests
chemistry, Domele, Fstuff, givensur, heshiming, HowEver, iAppDeveloper, iphonedevshani, jbro, JoeRCruso, kapps11, newDev, SLIC, stanny
Most users ever online was 1,387, 04-10-2012 at 04:21 AM.
» Stats
Members: 175,648
Threads: 94,112
Posts: 402,873
Top Poster: BrianSlick (7,990)
Welcome to our newest member, brandon6031
Powered by vBadvanced CMPS v3.1.0

All times are GMT -5. The time now is 07:25 PM.
Powered by vBulletin® Version 3.8.0
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.3.0