在做admob廣告時,橫向(Landscape)和直向(portrait)廣告大小不同,所以在進入viewDidLoad時需要知道是橫向或直向,但是一進viewDidLoad永遠是直向(portrait),用下方法測試可知:
if ([UIApplication sharedApplication].statusBarOrientation == UIInterfaceOrientationLandscapeLeft || [UIApplication sharedApplication].statusBarOrientation == UIInterfaceOrientationLandscapeRight)
{//iPad Landscape
NSLog(@"1. iPad Landscape -- viewDidLoad");
}
else{//iPad Portrait
NSLog(@"1. iPad Portrait -- viewDidLoad");
}
if ([self interfaceOrientation] == UIInterfaceOrientationPortrait || [self interfaceOrientation] == UIInterfaceOrientationPortraitUpsideDown)
{//iPad Landscape
NSLog(@"2. iPad Portrait -- viewDidLoad");
}
else{//iPad Portrait
NSLog(@"2. iPad landscape -- viewDidLoad");
}
if ([[UIDevice currentDevice] orientation] == UIDeviceOrientationLandscapeLeft || [[UIDevice currentDevice] orientation] == UIDeviceOrientationLandscapeRight )
{//iPad Landscape
NSLog(@"3. iPad landscape -- viewDidLoad");
}
else{//iPad Portrait
NSLog(@"3. iPad portrait -- viewDidLoad");
若device是iPad或device是iPad retina,且用iPad5.0 simulator,結果是:
1. iPad Portrait -- viewDidLoad
2. iPad Portrait -- viewDidLoad
3. iPad Portrait -- viewDidLoad
而且,iPad會轉成portrait。
若device是iPad或device是iPad retina,且用iPad6.0 simulator,結果是:
1. iPad Landscape -- viewDidLoad
2. iPad Landscape -- viewDidLoad
3. iPad portrait -- viewDidLoad
所以,if ([[UIDevice currentDevice] orientation] == UIDeviceOrientationLandscapeLeft || [[UIDevice currentDevice] orientation] == UIDeviceOrientationLandscapeRight )測試必定不對。
另兩個測試雖然在iPad6.0 simulator對了,但是viewDidLoad的view並未成為landscape,仍是768x1004。
所以需把與orientation相關之事放在willAnimateRotationToInterfaceOrientation做
- (void)willAnimateRotationToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation duration:(NSTimeInterval)duration
但是,這又發生一個問題,若iPad本為直向(portrait)或在iPad6.0 simulator下,則不會呼叫willAnimateRotationToInterfaceOrientation。
原始發現來自Link的answer6