UIView层次结构中的子视图不显示子视图

时间:2012-02-13 17:36:31

标签: iphone

执行此操作时,它会显示带有工具栏的全黑视图屏幕

- (void)displayviewsAction:(id)sender
{

self.view = [[[UIView alloc] initWithFrame:[[UIScreen mainScreen] bounds]]autorelease];

self.view.frame = CGRectMake(0, 0, 320, 480);

[self.view addSubview:toolbar];

UIView *viewController = [[UIView alloc] init];

UIView *secondController = [[UIView alloc] init];

[self.view insertSubview:viewController atIndex:1];

[self.view insertSubview:secondController atIndex:2];

NSLog(@"%d", [[self.view subviews] indexOfObject:viewController]); // Is 1

NSLog(@"%d", [[self.view subviews] indexOfObject:secondController]); // Is 2

[self.view bringSubviewToFront:viewController];

 NSLog(@"%d", [[self.view subviews] indexOfObject:viewController]);

[self.view sendSubviewToBack:viewController];

 NSLog(@"%d", [[self.view subviews] indexOfObject:viewController]);

[self.view bringSubviewToFront:secondController];

NSLog(@"%d", [[self.view subviews] indexOfObject:secondController]); 

SecondViewController * secondController = [[[SecondViewController alloc] init]autorelease];

secondController.view.frame = CGRectMake(0, 0, 320, 480);

[self.view addSubview:secondController.view];

[self.view addSubview:toolbar];

}

为什么视图显示为带工具栏的全黑色的任何想法。

1 个答案:

答案 0 :(得分:2)

视图的默认背景颜色为nil,这是一个透明背景。

如果您在任何视图中都没有任何内容,并且没有为其中任何一个设置任何背景颜色,则会显示窗口或设备的默认背景。我没有在文档中看到它,但我认为它是黑色的。

此外,您已定义了UIView指针,但使用“controller”一词命名。这可能会导致错误,因为UIViewController是一个完全不同的对象。 UIViewController包含一个UIView并与之交互,但它不是UIView,也不是从一个UIView继承。