在横向模式下启动应用程序 [英] Launching app in Landscape Mode

查看:92
本文介绍了在横向模式下启动应用程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个应用程序,我只想在Landscape中使用。

I have an app that I want to only work with in Landscape.

有史以来第一次,我是IB并试图设置所有我的以编程方式查看,所以我在loadView方法中创建一个视图并添加一堆子视图:

For the first time ever, I'm foregoing IB and trying to set up all my views programmatically, so I'm creating a view and adding a bunch of subviews in loadView method:

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];

在我的项目设置中,我已禁用两个纵向方向。我在根视图控制器中也有这个:

In my project settings, I have disabled both portrait orientations. I also have this in my root view controller:

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

-(UIInterfaceOrientation)preferredInterfaceOrientationForPresentation
{ 
    return UIInterfaceOrientationLandscapeRight;
}

我的问题是模拟器在景观中启动模式,但所有视图都是为肖像大小 - 所以我的视图的底部块位于屏幕下方,屏幕的右侧是一个很大的空白区域。

My problem is that the simulator starts in landscape mode, but all of the views are sized for portrait - so the bottom chunk of my views are below the screen and the right side of my screen is a big empty region.

我尝试通过在第一行中切换应用程序框架的宽度和高度来修复此问题,但随后在屏幕左边缘留下一些空的垂直空间用于状态栏。

I tried fixing this by switching the width and height of the application frame in the first line, but then that leaves some empty vertical room on the left edge of the screen for the status bar.

那么,我正在尝试做什么的正确方法是什么?

So, what's the correct way of doing what I'm trying to do?

推荐答案

而不是使用 [[UIScreen mainScreen] applicationFrame]

尝试使用 [[[[[self view] window] rootViewController] view] bounds]

边界将在横向方向正确表示宽度和高度,因为边界将考虑在内已应用的变换(旋转),而框架则不会。

The bounds will represent the width and height correctly in Landscape orientation, because the bounds will take into account the transform (rotation) that has been applied, while the frame will not.

要查看我的意思,请设置断点,并在调试器中打印出顶级视图的说明 lldb> po [[[[self view] window] rootViewController] view]

To see what I mean, set a breakpoint, and in the debugger print out the description of the top level view lldb> po [[[[self view] window] rootViewController] view]

你会看到视图有一个旋转变换,它的框架不代表横向屏幕的尺寸,而是以纵向表示尺寸!

You'll see that the view has a rotation transform and that its frame does not represent the dimensions of the screen in landscape, but represents the dimensions in portrait!

计算方法很长正确的applicationFrame将是

The long way to calculate the correct applicationFrame would be

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
    }
}

这篇关于在横向模式下启动应用程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆