在横向模式下启动应用程序

时间:2013-08-05 22:50:50

标签: ios ipad uiviewcontroller

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

有史以来第一次,我是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;
}

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

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

那么,做我正在做的事情的正确方法是什么?

3 个答案:

答案 0 :(得分:2)

而不是使用[[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
    }
}

答案 1 :(得分:0)

将它放在当前的viewcontroller中 // ios 5

 -(BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
    {


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

// ios6

-(NSUInteger)supportedInterfaceOrientations
{
    return UIInterfaceOrientationMaskLandscape;
}

答案 2 :(得分:0)

-(BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{


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

把这段代码放到Appdelegate .M .....