【发布时间】:2016-11-12 10:29:25
【问题描述】:
关于 ios 中的子视图,我有一个覆盖整个屏幕的视图,然后我创建并导入另一个视图,该视图顶部只是一个红色方块。我想知道这两种方法之间是否有任何区别或优势:
方法一:
//set the view
UIView *myView = [[UIView alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
myView.backgroundColor = [UIColor whiteColor];
[self.view addSubview:myView];
//setTheSquareView
CGRect firstFrame = CGRectMake(160, 240, 100, 150);
HypnosisView *firstView = [[HypnosisView alloc] initWithFrame:firstFrame];
firstView.backgroundColor = [UIColor redColor];
[self.view addSubview:firstView];
和方法2:
//set the view
UIView *myView = [[UIView alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
myView.backgroundColor = [UIColor whiteColor];
[self.view addSubview:myView];
//setTheSquareView
CGRect firstFrame = CGRectMake(160, 240, 100, 150);
HypnosisView *firstView = [[HypnosisView alloc] initWithFrame:firstFrame];
firstView.backgroundColor = [UIColor redColor];
[myView addSubview:firstView];
唯一的区别是,在第一种情况下,我将两个视图都添加为主属性视图的子视图,而在第二种情况下,我将第二个视图添加为第一个视图的子视图。它们在屏幕上看起来一样。 谢谢
【问题讨论】:
-
如果您的 myView 的框架与您的 self.view 的框架不同,它看起来会有所不同。因为视图的框架被调整为其直接父视图。
标签: ios objective-c uiview subview