在UIWindow上的偏移addSubview [英] Offset on UIWindow addSubview

查看:102
本文介绍了在UIWindow上的偏移addSubview的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个基于UITabBar的应用程序,工作正常。在某些情况下,我显示一个不同的UIViewController。现在我的错误是,我必须调整测试笔尖的框架(和只有测试笔尖!)才能正确显示。 (否则视图位于状态栏下方)。

I've got a UITabBar based application that works just fine. Under certain circumstances I am showing a different UIViewController instead though. Now what bugs me is that that I have to adjust the frame for the Test nib (and only the Test nib!) to display correctly. (Otherwise the view is below the status bar).

- (void)applicationDidFinishLaunching:(UIApplication *)application
{
    if (condition) {

        UIViewController *vc = [[UIViewController alloc] initWithNibName:@"Test" bundle:nil];

        // FIXME this should NOT be required
        CGRect r = vc.view.frame;
        r.origin.y += 20;
        vc.view.frame = r;

        [window addSubview:vc.view];
        [window makeKeyAndVisible];
        return;
    }

    [window addSubview:tabViewController.view];
    [window makeKeyAndVisible];
}

那么也许测试nib的错误?不能。测试笔尖在一个干净的新项目中根据需要工作。一个新的干净的笔尖显示相同的症状。所以东西一定是错误的MainWindow笔尖,对吧?但是UITabBarController显示得很好。

So maybe something is wrong with the Test nib? Can't be. The Test nib works as desired in a clean new project. And a new clean nib shows the same symptoms. So something must be wrong with the MainWindow nib, right? But the UITabBarController displays just fine.

我有点困惑,在这里没有想法。

I am a little confused and running out of ideas here. Any suggestions how to track this down?

推荐答案

添加根视图到你的UIWindow可能很复杂,因为窗口总是underlaps状态酒吧。因此,您的根视图的框架必须重置为 [[UIScreen mainScreen] applicationFrame] ,以防止它也重叠状态栏。我们通常不必担心这个,因为UIViewController修改框架为我们...除非它不。这是协议:

Adding the root view to your UIWindow can be complicated since the window always underlaps the status bar. The frame of your root view must therefore be reset to [[UIScreen mainScreen] applicationFrame] to prevent it from underlapping the status bar as well. We normally don't have to worry about this because UIViewController modifies the frame for us... except when it doesn't. Here's the deal:


  • 如果您在同一个NIB中创建视图控制器
    及其视图,嵌套视图
    控制器下方的视图, 将自动调整视图的框架。

  • 如果创建视图控制器和
    它的视图在同一NIB,但你
    通过控制器的
    视图出口连接视图到视图
    控制器,而不是嵌套它,控制器 <

  • 如果您在一个NIB中创建视图控制器,并将它连接到在一个分离的视图中定义的视图通过在IB中设置视图控制器的NIB名称属性,它将 自动调整视图的帧,但 仅当 / strong>。您还可以选中调整来自NIB的视图大小。

  • 如果通过调用-initWithNibName:bundles创建视图控制器,

  • UITabBarController期望将其视图添加为窗口的根视图,因此总是调整其自己的视图框架以匹配应用程序框架自动。 (因此,如果您将UITabBarController的视图添加为除窗口之外的任何子视图,则您会注意到一个奇怪的20像素间隙。)

  • If you create your view controller and its view in the same NIB, and you nest the view underneath the view controller, it will adjust the view's frame automatically.
  • If you create your view controller and its view in the same NIB, but you connect the view to the view controller through the controller's view outlet rather than nesting it, the controller will not adjust the view's frame automatically.
  • If you create your view controller in one NIB, and you connect it to a view defined in a detached NIB by setting the view controller's "NIB Name" property in IB, it will adjust the view's frame automatically, but only if you also have "Resize view from NIB" checked.
  • If you create your view controller by calling -initWithNibName:bundle:, it will not adjust the view's frame automatically.
  • UITabBarController expects its view to be added as the root view of the window, and therefore always adjusts its own view's frame to match the application frame automatically. (As a result you'll notice a weird 20 pixel gap if you ever add a UITabBarController's view as a subview of anything other than the window.)

我猜苹果认为-initWithNibName:bundle:通常不会用于创建窗口的根视图,所以它不会调整你的case的框架。您可以手动调整大小,但事实上您可以在查看控制器编程指南iPhone操作系统,但你应该真正使用 [[UIScreen mainScreen] applicationFrame] ,因为状态栏不是总是高度为20像素(例如,当您打电话时它的高度较高)。

I guess Apple figured that -initWithNibName:bundle: wouldn't typically be used to create the window's root view, so it doesn't adjust the frame in your case. Resizing it manually as you have done is fine, and is in fact recommended in the View Controller Programming Guide for iPhone OS, but you should really use [[UIScreen mainScreen] applicationFrame] since the status bar isn't always 20 pixels tall (e.g. it's taller when you're on a phone call.)

这篇关于在UIWindow上的偏移addSubview的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆