Device orientation at launch
I've read ALL of the posts on this topic, but still can't figure this one out.
I add a VC to the window in applicationDidFinishLaunching
. The VC contains a UIImageView.
I want to set the image of the UIImageView based on the device orientation, but statusBarOrientation
, deviceOrientation
, interfaceOrientation
, everything I check returns portrait.
There's gotta be something simple I'm miss开发者_Go百科ing.
Your help is appreciated.
Have you enabled the other orientations in your Info.plist? There's a graphical editor for this in Xcode 4's project editor.
Sometimes (not sure if this is in every case, or just the simulator), the app will launch in one orientation and then immediately rotate to another orientation to match the device orientation.
I would suggest implementing -[UIViewController willRotateToInterfaceOrientation:duration:]
to set your image, which should be called immediately after.
Well.. it seems there were several issues. 1) The simulator doesn't consistently generate orientation (or change in orientation) data as expected. Also, 2) immediately at start up, the device may (or may not) send statusBarOrientation info in time to read that. So, I did this right in the didFinishLaunching method in the app delegate.
// add the splash IV to the window per the current orientation, then animate it away
[[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications];
if([[UIDevice currentDevice] orientation] == UIDeviceOrientationPortrait){
self.theSplashIV.image = [UIImage imageNamed:@"Default-Portrait~ipad.png"];
self.theSplashIV.frame = CGRectMake(0, 20, 768, 1004);
}
else if([[UIDevice currentDevice] orientation] == UIDeviceOrientationPortraitUpsideDown){
self.theSplashIV.image = [UIImage imageNamed:@"Default-Portrait~ipad.png"];
self.theSplashIV.transform = CGAffineTransformMakeRotation(M_PI);
self.theSplashIV.frame = CGRectMake(0, 0, 768, 1004);
}
else if([[UIDevice currentDevice] orientation] == UIDeviceOrientationLandscapeLeft){
self.theSplashIV.image = [UIImage imageNamed:@"Default-Landscape~ipad.png"];
self.theSplashIV.transform = CGAffineTransformMakeRotation(M_PI / 2);
self.theSplashIV.frame = CGRectMake(0, 0, 748, 1024);
}
else if([[UIDevice currentDevice] orientation] == UIDeviceOrientationLandscapeRight){
self.theSplashIV.image = [UIImage imageNamed:@"Default-Landscape~ipad.png"];
self.theSplashIV.transform = CGAffineTransformMakeRotation(M_PI / -2);
self.theSplashIV.frame = CGRectMake(20, 0, 748, 1024);
}
[[UIDevice currentDevice] endGeneratingDeviceOrientationNotifications];
And then I animate away the splash IV with a delay. Not very elegant, but it works.
精彩评论