在横向模式下启动应用程序
我有一个应用程序,我只想在景观工作。
有史以来第一次,我是前面的IB,并试图以编程方式设置我的所有视图,所以我创build一个视图,并在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; }
我的问题是,模拟器以横向模式启动 ,但所有的视图大小为肖像 – 所以我的意见的底部块在屏幕下方,我的屏幕右侧是一个很大的空白区域。
我试图通过在第一行中切换应用程序框架的宽度和高度来解决这个问题,但是之后在状态栏的屏幕左边留下了一些空的垂直空间。
那么,我正在做什么的正确方法是什么?
而不是使用[[UIScreen mainScreen] applicationFrame]
尝试使用[[[[[self view] window] rootViewController] view] bounds]
边界将在横向方向上正确表示宽度和高度,因为边界将考虑已应用的变换(旋转),而帧不会。
为了明白我的意思,设置一个断点,并在debugging器中打印出顶层视图的描述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 } }
-(BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation { if (UIInterfaceOrientationIsLandscape( interfaceOrientation)) { return YES; } return NO; }
把这个代码Appdelegate .M …..
把它放在当前的viewcontroller中// ios 5
-(BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation { if (UIInterfaceOrientationIsLandscape( interfaceOrientation)) { return YES; } return NO; }
// ios6
-(NSUInteger)supportedInterfaceOrientations { return UIInterfaceOrientationMaskLandscape; }