两种不同的子视图方法

Two different approaches to subviews

关于 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) 约束,firstFrame 不会改变,但在第二种方法中,它会改变。