Objective c 如何获得正确方向的全屏边界?

Objective c 如何获得正确方向的全屏边界?,objective-c,orientation,bounds,cgrect,Objective C,Orientation,Bounds,Cgrect,伙计们。 我有这样的问题:我需要使用全屏查看。所以我用 [_activityIndicator setFrame:[[UIScreen mainScreen] applicationFrame]]; 这是完美的作品,但只有在纵向视图。如果我的应用程序也必须在横向环境中工作,该怎么办?要获取当前屏幕边界,我使用: CGRect screenBoundsDependOnOrientation() { CGRect screenBounds = [UIScreen mainScreen].b

伙计们。 我有这样的问题:我需要使用全屏查看。所以我用

[_activityIndicator setFrame:[[UIScreen mainScreen] applicationFrame]];

这是完美的作品,但只有在纵向视图。如果我的应用程序也必须在横向环境中工作,该怎么办?

要获取当前屏幕边界,我使用:

CGRect screenBoundsDependOnOrientation()
{
    CGRect screenBounds = [UIScreen mainScreen].bounds ;
    CGFloat width = CGRectGetWidth(screenBounds)  ;
    CGFloat height = CGRectGetHeight(screenBounds) ;
    UIInterfaceOrientation interfaceOrientation = [UIApplication sharedApplication].statusBarOrientation;

    if(UIInterfaceOrientationIsPortrait(interfaceOrientation)){
        screenBounds.size = CGSizeMake(width, height);
    }else if(UIInterfaceOrientationIsLandscape(interfaceOrientation)){
        screenBounds.size = CGSizeMake(height, width);
    }
    return screenBounds ;
}

要获取当前屏幕边界,我使用:

CGRect screenBoundsDependOnOrientation()
{
    CGRect screenBounds = [UIScreen mainScreen].bounds ;
    CGFloat width = CGRectGetWidth(screenBounds)  ;
    CGFloat height = CGRectGetHeight(screenBounds) ;
    UIInterfaceOrientation interfaceOrientation = [UIApplication sharedApplication].statusBarOrientation;

    if(UIInterfaceOrientationIsPortrait(interfaceOrientation)){
        screenBounds.size = CGSizeMake(width, height);
    }else if(UIInterfaceOrientationIsLandscape(interfaceOrientation)){
        screenBounds.size = CGSizeMake(height, width);
    }
    return screenBounds ;
}

你的应用程序有一个根视图控制器,它根据设备旋转而旋转。因此,根据根视图控制器的视图设置框架。使用其边界,而不是其框架

您可以通过主窗口获取对根视图控制器的引用。您可以通过共享应用程序获取对主窗口的引用:

UIWindow* theWindow = [[UIApplication sharedApplication] keyWindow];
UIViewController* rvc = theWindow.rootViewController;
UIView* mainView = rvc.view;
UIActivityIndicatorView* act = 
    [[UIActivityIndicatorView alloc] initWithFrame: mainView.bounds];
[mainView addSubview: act];

然后在属性中保留对活动指示器的引用,并将其设置为旋转。

您的应用程序具有根视图控制器,该控制器根据设备旋转而旋转。因此,根据根视图控制器的视图设置框架。使用其边界,而不是其框架

您可以通过主窗口获取对根视图控制器的引用。您可以通过共享应用程序获取对主窗口的引用:

UIWindow* theWindow = [[UIApplication sharedApplication] keyWindow];
UIViewController* rvc = theWindow.rootViewController;
UIView* mainView = rvc.view;
UIActivityIndicatorView* act = 
    [[UIActivityIndicatorView alloc] initWithFrame: mainView.bounds];
[mainView addSubview: act];

然后在属性中保留对活动指示器的引用,并将其设置为旋转。

效果很好!谢谢<代码>[[U activityIndicator setFrame:[[UIApplication sharedApplication]keyWindow]rootViewController].view.bounds];[[[UIApplication sharedApplication]keyWindow]根视图控制器].view添加子视图:[U activityIndicator]完美答案,但很抱歉我不能给你+1。声誉==8:)没问题,谢谢你的勾选!如果你刚刚开始,我的书有一个在线免费版本。了解框架和边界:了解视图控制器和旋转:效果不错!谢谢<代码>[[U activityIndicator setFrame:[[UIApplication sharedApplication]keyWindow]rootViewController].view.bounds];[[[UIApplication sharedApplication]keyWindow]根视图控制器].view添加子视图:[U activityIndicator]完美答案,但很抱歉我不能给你+1。声誉==8:)没问题,谢谢你的勾选!如果你刚刚开始,我的书有一个在线免费版本。了解帧和边界的步骤:了解视图控制器和旋转的步骤: