您现在的位置: IOS教程網 >> IOS系統 >  >> iOS 程式開發 >> iOS 開發綜合

iOS 7 中實作子視圖旋轉方向控制

編輯:iOS 開發綜合

為了讓父視圖(地圖視圖)能隨意旋轉,而子視圖按某種固定方向(豎屏)顯示,我找了好些方案,最後發現在 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;
}
  1. 上一篇文章:
  2. 下一篇文章:
蘋果刷機越獄教程| IOS教程問題解答| IOS技巧綜合| IOS7技巧| IOS8教程
Copyright © IOS教程網 All Rights Reserved