1

我有一个我只想在 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;
}

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

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

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

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

把它放在当前视图控制器中 // ios 5

 -(BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
    {


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

// ios6

-(NSUInteger)supportedInterfaceOrientations
{
    return UIInterfaceOrientationMaskLandscape;
}
于 2013-08-06T04:49:44.007 回答
0
-(BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{


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

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

于 2013-08-06T04:59:53.547 回答