到目前为止,我有以下代码:
float xCenter;
float yCenter;
if (self.splitViewController.interfaceOrientation == UIInterfaceOrientationPortrait || self.splitViewController.interfaceOrientation == UIInterfaceOrientationPortraitUpsideDown){
xCenter = 384;
yCenter = 512;
} else if (self.splitViewController.interfaceOrientation == UIInterfaceOrientationLandscapeLeft || self.splitViewController.interfaceOrientation == UIInterfaceOrientationLandscapeRight){
xCenter = 512;
yCenter = 384;
}
uinc.view.superview.center = CGPointMake(xCenter, yCenter);
有没有更好的方法来简化这个?我想做的就是将其显示在屏幕中央。我尝试将其分配给 `self.splitViewController.view.center,但它没有给我想要的中心。
uinc 是 UIModalPresentationSheet 中呈现的 UINavigationController,但我想更改它的大小,因此必须设置位置。
最佳答案
您可以从使用 UIInterfaceOrientationIs*
方法开始。
这是重写该代码片段的一种方法:
CGPoint centerPoint = (UIInterfaceOrientationIsPortrait(self.splitViewController.interfaceOrientation) ?
CGPointMake(384, 512) : CGPointMake(512, 384));
您可以通过 [UIWindow mainWindow]
的 bounds
或 applicationFrame
方法检索屏幕中心,而不是硬编码中心点(不考虑方向)。
另一个解决方案是使用 UISplitViewController View 的中心:
UIViewController *splitViewController = [[[UIApplication sharedApplication] delegate] splitViewController];
CGRect bounds = [splitViewController.view bounds];
CGPoint centerPoint = CGPointMake(CGRectGetMidX(bounds), CGRectGetMidY(bounds));
如果您尝试创建加载指示器或其他应显示在屏幕上所有其他内容之上的元素,则应考虑将其直接添加到应用程序的 UIWindow 中。在这种情况下,您将需要检测界面方向并相应地旋转您的 View 。
UIInterfaceOrientation orientation = [UIApplication sharedApplication].statusBarOrientation;
我建议查看 MBProgressHUD 的来源它使用这种方法(作为可配置选项)。
关于objective-c - 根据方向获取 iPad View 的中心,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6160142/