1

ランドスケープでのみ操作したいアプリがあります。

初めて IB に先んじて、すべてのビューをプログラムでセットアップしようとしているので、ビューを作成し、loadView メソッドに一連のサブビューを追加しています。

self.view = [[UIView alloc] initWithFrame:[[UIScreen mainScreen] applicationFrame]];

// Create a GMSCameraPosition that tells the map to display the
// coordinate -33.86,151.20 at zoom level 6.
GMSCameraPosition *camera = [GMSCameraPosition cameraWithLatitude:-33.86
                                                        longitude:151.20
                                                             zoom:6];

self.mapView = [GMSMapView mapWithFrame:CGRectZero camera:camera];
self.mapView.myLocationEnabled = YES;
self.mapView.delegate = self;
self.mapView.mapType = kGMSTypeHybrid;
self.mapView.frame = self.view.frame;
[self.view addSubview:self.mapView];

// add the toolbar
UIToolbar* toolbar = [[UIToolbar alloc] init];
toolbar.frame = CGRectMake(0, self.view.frame.size.height - 44, self.view.frame.size.width, 44);
toolbar.barStyle = UIBarStyleDefault;
NSMutableArray* items = [NSMutableArray array];
[items addObject:[[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"location-arrow.png"]
                                                  style:UIBarButtonItemStyleBordered
                                                 target:self
                                                 action:@selector(locateMe:)]];
[items addObject:[[UIBarButtonItem alloc] initWithTitle:@"Tools"
                                                  style:UIBarButtonItemStyleBordered
                                                 target:self
                                                 action:@selector(toolsButtonTapped:)]];
[toolbar setItems:items];
[self.view addSubview:toolbar];

私のプロジェクト設定では、両方の縦向きを無効にしました。ルートビューコントローラーにもこれがあります:

// Enforce Landscape Orientation
-(NSUInteger)supportedInterfaceOrientations
{
    return UIInterfaceOrientationMaskLandscape;
}

-(UIInterfaceOrientation)preferredInterfaceOrientationForPresentation
{ 
    return UIInterfaceOrientationLandscapeRight;
}

私の問題は、シミュレーターが横向きモードで起動することですが、すべてのビューが縦向きのサイズになっているため、ビューの下部のチャンクが画面の下にあり、画面の右側が大きな空の領域です。

最初の行でアプリケーション フレームの幅と高さを切り替えることでこれを修正しようとしましたが、ステータス バーの画面の左端に空の垂直方向の部屋が残ります。

それで、私がやろうとしていることをする正しい方法は何ですか?

4

3 に答える 3

3

使用する代わりに[[UIScreen mainScreen] applicationFrame]

使ってみて[[[[[self view] window] rootViewController] view] bounds]

境界は適用された変換 (回転) を考慮に入れますが、フレームは考慮しないため、横向きの場合、境界は幅と高さを正しく表します。

意味を理解するには、ブレークポイントを設定し、デバッガーでトップ レベル ビューの説明を出力します。lldb> po [[[[self view] window] rootViewController] view]

ビューに回転変換があり、そのフレームが横向きの画面の寸法を表しているのではなく、縦向きの寸法を表していることがわかります!


正しい applicationFrame を計算する長い道のりは次のようになります。

BOOL iOS7 = NO;
NSString *currSysVer = [[UIDevice currentDevice] systemVersion];
if ([currSysVer compare:@"7.0" options:NSNumericSearch] != NSOrderedAscending)
    iOS7 = YES;

CGRect theFrame;
CGRect statusBarFrame = [[UIApplication sharedApplication] statusBarFrame];
CGRect screenBounds = [[UIScreen mainScreen] bounds];

if (UIInterfaceOrientationIsLandscape([[UIApplication sharedApplication] statusBarOrientation])) {

    theFrame.origin = CGPointZero;
    theFrame.size.width = screenBounds.size.height;
    theFrame.size.height = screenBounds.size.width;

    if (iOS7 == NO) {
        // statusBarFrame will be CGRectZero if not visible, so this is safe
        theFrame.size.height -= statusBarFrame.size.width;  // because we're in landscape orientation
    }
}
else {

    theFrame = screenBounds;

    if (iOS7 == NO) {
        // statusBarFrame will be CGRectZero if not visible, so this is safe
        theFrame.size.height -= statusBarFrame.size.height;  // because we're in portrait orientation
    }
}
于 2013-08-06T04:58:43.697 に答える
0
-(BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{


    if (UIInterfaceOrientationIsLandscape( interfaceOrientation)) 
    {
        return  YES;
    }
    return NO;
}

このコードを Appdelegate .M.....

于 2013-08-06T04:59:53.547 に答える