如何获得与方向相关的屏幕高度和宽度?

我正在尝试以编程方式确定应用程序的当前高度和宽度。我用这个:


CGRect screenRect = [[UIScreen mainScreen] bounds];

但这将产生320的宽度和480的高度,而不管设备是纵向还是横向。如何确定主屏幕的当前宽度和高度(即取决于设备方向)?


斯蒂芬大帝
浏览 557回答 3
3回答

千巷猫影

在iOS 8+中,您应该使用以下viewWillTransitionToSize:withTransitionCoordinator方法:-(void)viewWillTransitionToSize:(CGSize)size withTransitionCoordinator:(id<UIViewControllerTransitionCoordinator>)coordinator {&nbsp; &nbsp; [super viewWillTransitionToSize:size withTransitionCoordinator:coordinator];&nbsp; &nbsp; // You can store size in an instance variable for later&nbsp; &nbsp; currentSize = size;&nbsp; &nbsp; // This is basically an animation block&nbsp; &nbsp; [coordinator animateAlongsideTransition:^(id<UIViewControllerTransitionCoordinatorContext> context) {&nbsp; &nbsp; &nbsp; &nbsp; // Get the new orientation if you want&nbsp; &nbsp; &nbsp; &nbsp; UIInterfaceOrientation orientation = [[UIApplication sharedApplication] statusBarOrientation];&nbsp; &nbsp; &nbsp; &nbsp; // Adjust your views&nbsp; &nbsp; &nbsp; &nbsp; [self.myView setFrame:CGRectMake(0, 0, size.width, size.height)];&nbsp; &nbsp; } completion:^(id<UIViewControllerTransitionCoordinatorContext> context) {&nbsp; &nbsp; &nbsp; &nbsp; // Anything else you need to do at the end&nbsp; &nbsp; }];}这将替换不推荐使用的动画方法,该方法不提供有关大小的信息:-(void)willAnimateRotationToInterfaceOrientation:(UIInterfaceOrientation)orientation duration:(NSTimeInterval)duration

慕莱坞森

您可以使用类似方法UIInterfaceOrientationIsPortrait([UIApplication sharedApplication].statusBarOrientation)来确定方向,然后相应地使用尺寸。但是,在方向更改期间(如UIViewController的- (void) willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;duration:(NSTimeInterval)duration使用传入的方向,toInterfaceOrientation因为UIApplication的statusBarOrientation仍会指向旧的方向,因为它尚未更改(因为您位于will事件处理程序中)。摘要有几个与此相关的帖子,但是似乎每个帖子都表明您必须:看一下[[UIScreen mainScreen] bounds]尺寸检查您所处的方向,然后考虑状态栏的高度(如果显示)链接iPhone:获取当前视图尺寸或屏幕尺寸iPhone / iPad:如何以编程方式获取屏幕宽度?目标C-如何获得当前的屏幕分辨率?在iPhone或iPad中重新定向后,“框架” /窗口大小“不正确”iPhone Dev SDK-获取屏幕宽度工作守则我通常不会走那么远,但是您激起了我的兴趣。以下代码可以解决问题。我在UIApplication上写了一个Category。我添加了类方法来获取currentSize或给定方向的大小,这就是您在UIViewController中调用的方法willRotateToInterfaceOrientation:duration:。@interface UIApplication (AppDimensions)+(CGSize) currentSize;+(CGSize) sizeInOrientation:(UIInterfaceOrientation)orientation;@end@implementation UIApplication (AppDimensions)+(CGSize) currentSize{&nbsp; &nbsp; return [UIApplication sizeInOrientation:[UIApplication sharedApplication].statusBarOrientation];}+(CGSize) sizeInOrientation:(UIInterfaceOrientation)orientation{&nbsp; &nbsp; CGSize size = [UIScreen mainScreen].bounds.size;&nbsp; &nbsp; UIApplication *application = [UIApplication sharedApplication];&nbsp; &nbsp; if (UIInterfaceOrientationIsLandscape(orientation))&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; size = CGSizeMake(size.height, size.width);&nbsp; &nbsp; }&nbsp; &nbsp; if (application.statusBarHidden == NO)&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; size.height -= MIN(application.statusBarFrame.size.width, application.statusBarFrame.size.height);&nbsp; &nbsp; }&nbsp; &nbsp; return size;}@end要使用代码简单调用[UIApplication currentSize]。另外,我运行了上面的代码,因此我知道它可以工作并向各个方向报告正确的响应。请注意,我考虑了状态栏。有趣的是,我必须减去状态栏的高度和宽度的最小值。希望这可以帮助。:D其他想法您可以通过查看UIWindow的rootViewController属性来获取尺寸。我过去已经看过它,并且类似地在纵向和横向上报告了相同的尺寸,只是报告了具有旋转变换:(gdb) po [[[[[UIApplication sharedApplication] keyWindow] rootViewController]视图]<UILayoutContainerView:0xf7296f0; 帧=(0 0; 320480); 变换= [0,-1,1,0,0,0]; 自动调整大小= W + H; 层= <CALayer:0xf729b80 >>(gdb) po [[[[[UIApplication sharedApplication] keyWindow] rootViewController]视图]<UILayoutContainerView:0xf7296f0; 帧=(0 0; 320480); 自动调整大小= W + H; 层= <CALayer:0xf729b80 >>不确定应用程序的工作方式,但是如果您不使用某种导航控制器,则可以在主视图下有一个具有最大父级高度/宽度的UIView并随父级增大/缩小。然后,您可以这样做:[[[[[[[UIApplication sharedApplication] keyWindow] rootViewController] view] subviews] objectAtIndex:0] frame]。一方面看起来很激烈,但是您明白了。但是...最好在摘要下执行上述三个步骤。开始弄乱UIWindows,您会发现奇怪的东西,例如显示UIAlertView将更改UIApplication的键窗口以指向由UIAlertView创建的新UIWindow。谁知道?我发现了一个依赖keyWindow的错误并发现它像那样改变后才做了!
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

iOS