objective-c - 根据方向获取 iPad View 的中心

标签 objective-c ipad uisplitviewcontroller

到目前为止,我有以下代码:

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]boundsapplicationFrame 方法检索屏幕中心,而不是硬编码中心点(不考虑方向)。


另一个解决方案是使用 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/

相关文章:

ios - UICollectionView allowsMultipleSelections 不工作

ios - 无论如何将 NSString 设置为指向另一个字符串的指针?

iphone - UIAlertView 中 UIView 中的 UITextField 不起作用?

iphone - 设置 UIWebView scrollView delegate 导致错误

iphone - 如何在 iOS 视网膜和非视网膜屏幕底部放置 admob 横幅

objective-c - Apple 是否提供用于编辑/添加提醒的 UI?

ios - 如果我误删除了MainStoryboard_iPad,如何制作通用应用程序

ios - 隐藏纵向的 UISplitViewController 覆盖

iphone - iOS - 更改 masterViewController 的大小

ios - 对 iPhone 6+ 旋转使用react——traitCollectionDidChange : called twice for nav controller