代码之家  ›  专栏  ›  技术社区  ›  jbrennan

在横向应用程序启动(iPhone/iPad)时,uiscreen applicationframe返回不正确的矩形

  •  21
  • jbrennan  · 技术社区  · 15 年前

    在横向模式下启动iPad应用程序时,无法获得正确的界限。我在info.plist文件中设置了正确的键,视图控制器在横向(和纵向,natch)中正常启动。

    在我的 applicationDidFinishLaunching: 方法在3秒延迟后调用选择器,该方法调用 [[UIScreen mainScreen] applicationFrame] ,但它会返回一个肖像框(即高度和宽度)。

    有人知道怎么修理这个吗?在我看来,它闻起来像个虫子(如果是的话,我会提交一个雷达),但如果它是预期的行为,它在哪里被记录下来?

    8 回复  |  直到 11 年前
        1
  •  6
  •   progrmr    15 年前

    当你将iPad放在横向方向并启动应用程序时,视图控制器最初会看到纵向视图的边界(即使方向报告横向)。然后,视图控制器将收到一条消息,在它出现之前旋转到横向。

        2
  •  35
  •   bentford Marko Hlebar    14 年前

    我从不依赖 [[UIScreen mainScreen] applicationFrame] 尤其是在应用程序发布期间。

    在代码中创建视图时,请使用超级视图设置框架。

    如果您将XIB与“模拟接口元素”结合使用,那么它们的大小将正确,并且一切都会很好地工作。

    基于uinavigationController的应用程序

    对于基于uinavigationController的应用程序,直接从 self.navigationController.view ,不要试图使用 [self loadView] self.view.superview . uinavigationcontroller使用“隐藏的”子视图来完成它的工作——因此直接的超级视图将无法工作。

    uinavigationcontroller是特殊的,因为在应用程序启动期间,导航控制器会在 loadView 被呼叫。当自动调整开始时,屏幕底部会有一个小的空白。

    为什么不uiscreen

    [[uiscreen主屏幕]应用程序框架] 工作不可靠(尤其是在横向应用程序发布期间)。我的经验是 interfaceOrientation 属性将不匹配 applicationFrame 方向。

        3
  •  14
  •   100grams    12 年前
    CGRect bounds = [[UIScreen mainScreen] bounds]; // portrait bounds
    if (UIInterfaceOrientationIsLandscape([[UIApplication sharedApplication] statusBarOrientation])) {
        bounds.size = CGSizeMake(bounds.size.height, bounds.size.width);
    }
    
        4
  •  6
  •   Francesco Puglisi    13 年前

    这是当视图控制器处于横向时获得正确cgrect的方法:

    CGRect landscapeBounds;
    if (self.view.frame.size.width < self.view.frame.size.height)
        landscapeBounds = CGRectMake(self.view.frame.origin.y, self.view.frame.origin.x, self.view.frame.size.height, self.view.frame.size.width);
    else
        landscapeBounds = CGRectMake(self.view.frame.origin.x, self.view.frame.origin.y, self.view.frame.size.width, self.view.frame.size.height);
    
        5
  •  5
  •   rpetrich    15 年前

    这是按设计的。您应该查询超级视图的大小,并根据需要进行调整。

        6
  •  2
  •   Hejazi    11 年前

    [[UIScreen mainScreen] applicationFrame] 即使应用程序处于横向模式,也将始终返回纵向矩形。这与以下事实有关: UIWindow 从未 实际上是旋转的,但只改变 rootViewController.view 相反。

    要确保这一点,您可以在纵向和横向模式下打印根视图对象,您将看到如下内容:

    肖像:

    <UIView: 0x96290e0; frame = (0 20; 768 1004); ...
    

    景观:

    <UIView: 0x96290e0; frame = (0 20; 768 1004); transform = [0, 1, -1, 0, 0, 0]; ...
    
        7
  •  1
  •   Stephen J    11 年前

    因此,根据苹果的指南,添加一个发布图片并给它加上后缀-568h。

    我不明白为什么任何一个头脑健全的人都会让系统设置依赖于图形;但我只是测试了一下,它工作了。

    Here is the spot that taught me after a quick search 我在上面没有看到这个答案,我认为它对某人有用。

    S

        8
  •  0
  •   lietus    12 年前

    我在用在Cordova插件中动画的DismissViewController拒绝视图时遇到了同样的问题。 我在MainViewController中修改了viewWillAppear方法的singingatom代码,它在取消模式视图后得到了调整:

    - (void)viewWillAppear:(BOOL)animated
        {
        CGRect appFrame = [[UIScreen mainScreen] applicationFrame];
        UIDeviceOrientation orientation = [[UIApplication sharedApplication] statusBarOrientation];
        if (UIDeviceOrientationLandscapeLeft == orientation || 
            UIDeviceOrientationLandscapeRight == orientation)
                {
                if (appFrame.size.width < appFrame.size.height)
                    {
                    appFrame = CGRectMake(appFrame.origin.y, appFrame.origin.x, appFrame.size.height, appFrame.size.width);
                    }
            }
        self.view.frame = appFrame;
        [super viewWillAppear:animated];
    }