开发者

getting the center of iPad view depending on orientation

As of now I have the following code:

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);

Is there a better way to simplify this? All I want to do is to show this at the center of the screen. I tried assigning it to `self.splitViewController.view.center, but it does't give me the center as I want.

uinc is a UINavigationController presented in UIModalPresentationSheet, b开发者_如何学JAVAut I want to change the size of it and therefore have to set the location.


You could start by using the UIInterfaceOrientationIs* methods.

Here is one way you could rewrite that snippet:

CGPoint centerPoint = (UIInterfaceOrientationIsPortrait(self.splitViewController.interfaceOrientation) ?
                       CGPointMake(384, 512) : CGPointMake(512, 384));

Instead of hard-coding center points you could retrieve the center of the screen via the bounds or applicationFrame methods of [UIWindow mainWindow] (which do not take orientation into account).


Another solution would be to use the center of your UISplitViewController's view:

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

If you are attempting to create a loading indicator or some other element that should appear on top of everything else on the screen, you should consider adding it directly to your app's UIWindow. In this case you will need to detect the interface orientation and rotate your view accordingly.

UIInterfaceOrientation orientation = [UIApplication sharedApplication].statusBarOrientation;

I suggest looking at the source of MBProgressHUD which uses this approach (as a configurable option).

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜