2013-08-05 156 views
0

启动应用程序我有,我只想与景观工作的应用程序。在横向模式下

这是第一次,我在前述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; 
} 

我的问题是在横向模式下开始,但所有的意见是尺寸适于人像模拟器 - 所以我的看法底部块低于屏幕和我的屏幕右侧是一个很大的空白区域。

我试图通过切换应用框架的宽度和高度在第一线固定这一点,但然后离开屏幕的状态栏的左边缘一些空垂直空间。

那么,有什么做什么,我试图做的正确方法是什么?

+0

只是想知道,在测试时会发生什么设备? – Squatch

+0

你可以尝试'self.view.transform = CGAffineTransformMakeRotation(M_PI/2);' –

+0

@Squatch:这是一个新客户端的应用程序,所以它是用他们的帐户创建的,我不能以成员身份加入直到会员中心恢复正常,所以我无法在设备上运行它。 – Shinigami

回答

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

'[[[[self view] window] rootViewController] view]'是'self。view'。我不应该只是在窗口的边界?无论如何,我会放弃它。 – Shinigami

+0

在上面添加了更多细节。希望这可以帮助。 –

+0

长计算完美。谢谢。 – Shinigami

0

在当前视图 - 控制将这个 // IOS 5

-(BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation 
    { 


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

// iOS6的

-(NSUInteger)supportedInterfaceOrientations 
{ 
    return UIInterfaceOrientationMaskLandscape; 
} 
+0

这在iOS 6中已弃用。Xcode说我应该使用我正在使用的2种方法。 – Shinigami

+0

检查更新的答案 –

+0

这就是我在我的问题有什么了。 – Shinigami

0
-(BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation 
{ 


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

将这个代码的appdelegate .M .....