Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/objective-c/27.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Objective c 根据方向获取iPad视图的中心_Objective C_Ipad_Uisplitviewcontroller - Fatal编程技术网

Objective c 根据方向获取iPad视图的中心

Objective c 根据方向获取iPad视图的中心,objective-c,ipad,uisplitviewcontroller,Objective C,Ipad,Uisplitviewcontroller,到目前为止,我有以下代码: float xCenter; float yCenter; if (self.splitViewController.interfaceOrientation == UIInterfaceOrientationPortrait || self.splitViewController.interfaceOrientation == UIInterfaceOrientationPortraitUpsideDown){ xCenter = 3

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

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]
边界
应用程序框架
方法(不考虑方向)检索屏幕中心点,而不是硬编码中心点


另一种解决方案是使用UISplitViewController视图的中心:

UIViewController *splitViewController = [[[UIApplication sharedApplication] delegate] splitViewController];
CGRect bounds = [splitViewController.view bounds];
CGPoint centerPoint = CGPointMake(CGRectGetMidX(bounds), CGRectGetMidY(bounds));

如果您试图创建一个加载指示符或其他应该出现在屏幕上其他元素之上的元素,则应该考虑将其直接添加到应用程序的UIWindows中。在这种情况下,需要检测界面方向并相应地旋转视图

UIInterfaceOrientation orientation = [UIApplication sharedApplication].statusBarOrientation;

我建议查看使用此方法的源代码(作为一个可配置选项)。

我在您的第一个解决方案中遇到一个错误,“使用不兼容类型CGPoint的表达式初始化CGPoint…”@adit:感谢您捕捉到这一点。修正。@adit:如果你告诉我为什么它不起作用,我也许能帮上忙。@adit:当方向改变时,你需要更新模式表的位置。