為了讓父視圖(地圖視圖)能隨意旋轉,而子視圖按某種固定方向(豎屏)顯示,我找了好些方案,最後發現在 iOS 7 中其實很簡單:
-(BOOL)shouldAutorotate {
if ([[UIDevice currentDevice] orientation] == UIInterfaceOrientationLandscapeLeft ||
[[UIDevice currentDevice] orientation] == UIInterfaceOrientationLandscapeRight) {
return NO;
} else {
return YES;
}
}
另外,有人在 iOS 6 中使用如下方式(動態取得螢幕尺寸並依方向調整寬高):
static CGSize appScreenSize;
static UIInterfaceOrientation lastOrientation;
+ (CGSize)screenSize {
UIInterfaceOrientation orientation = [UIApplication sharedApplication].statusBarOrientation;
if (appScreenSize.width == 0 || lastOrientation != orientation) {
appScreenSize = CGSizeMake(0, 0);
CGSize screenSize = [[UIScreen mainScreen] bounds].size;
// 這裡若去掉狀態欄,只要用 applicationFrame 即可
if (orientation == UIDeviceOrientationLandscapeLeft || orientation == UIDeviceOrientationLandscapeRight) {
// 橫屏時,返回的寬度應該是系統給的高度
// 注意:全屏與非全屏應用應自行增減狀態欄高度
appScreenSize.width = screenSize.height;
appScreenSize.height = screenSize.width;
} else {
appScreenSize.width = screenSize.width;
appScreenSize.height = screenSize.height;
}
lastOrientation = orientation;
}
return appScreenSize;
}