为什么我使用Addsubview添加其他控制器的视图,视图将向下移动20个点?

时间:2011-06-25 09:39:17

标签: iphone uiviewcontroller addsubview

我正在使用下面的代码来测试问题。

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    UIViewController *controller = [[UIViewController alloc] init];
    controller.view.backgroundColor = [UIColor whiteColor];
    [window setRootViewController:controller];
    UIViewController *controller2 = [[UIViewController alloc] init];
    [controller.view addSubview:controller2.view];
    controller2.view.backgroundColor = [UIColor yellowColor];
    UIViewController *controller3 = [[UIViewController alloc] init];
    controller3.view.backgroundColor = [UIColor purpleColor];
    [controller2.view addSubview:controller3.view];
    [window makeKeyAndVisible];
    return YES;
}

当我在模拟器或iphone上运行它时,它会出现 image
我很困惑,因为这个问题出现在我的项目中,但是当我使用系统的照片选择器时,它会显示正确。 所以,谁能帮助我,非常感谢你。

1 个答案:

答案 0 :(得分:1)

默认情况下,UIViewController的{​​{1}}使用view值作为其框架。这通常会占据状态栏,并且为[[UIScreen mainScreen] applicationFrame]。因此,当您按层次添加视图时,您会发现视图向下推。