iOS 設(shè)置view的層級關(guān)系

1.設(shè)置某view到最上層

// 初始化第一個view并添加到當(dāng)前控制器的view上;

UIView *first = [[UIView alloc] initWithFrame:CGRectMake(30, 30, 100, 100)];

first.backgroundColor = [UIColor redColor];

[self.view addSubview:first];

// 初始化第二個view并添加到當(dāng)前控制器的view上;

UIView *second = [[UIView alloc] initWithFrame:CGRectMake(50, 50, 100, 100)];

second.backgroundColor = [UIColor greenColor];

[self.view addSubview:second];

// 設(shè)置第一個view到最上層

[self.view bringSubviewToFront:first];


2.設(shè)置某view到最下層

// 初始化第一個view并添加到當(dāng)前控制器的view上;

UIView *first = [[UIView alloc] initWithFrame:CGRectMake(30, 30, 100, 100)];

first.backgroundColor = [UIColor redColor];

[self.view addSubview:first];

// 初始化第二個view并添加到當(dāng)前控制器的view上;

UIView *second = [[UIView alloc] initWithFrame:CGRectMake(50, 50, 100, 100)];

second.backgroundColor = [UIColor greenColor];

[self.view addSubview:second];

// 初始化第三個view并添加到當(dāng)前控制器的view上;

UIView *third = [[UIView alloc] initWithFrame:CGRectMake(70, 70, 100, 100)];

third.backgroundColor = [UIColor yellowColor];

[self.view addSubview:third];

[self.view sendSubviewToBack:second];

// 設(shè)置第二個view到最下層

[self.view sendSubviewToBack:second];


3.設(shè)置某view到指定層

// 初始化第一個view并添加到當(dāng)前控制器的view上;

UIView *first = [[UIView alloc] initWithFrame:CGRectMake(30, 30, 100, 100)];

first.backgroundColor = [UIColor redColor];

[self.view addSubview:first];

// 初始化第二個view并添加到當(dāng)前控制器的view上;

UIView *second = [[UIView alloc] initWithFrame:CGRectMake(50, 50, 100, 100)];

second.backgroundColor = [UIColor greenColor];

[self.view addSubview:second];

// 初始化第三個view并添加到當(dāng)前控制器的view上;

UIView *third = [[UIView alloc] initWithFrame:CGRectMake(30, 70, 100, 100)];

third.backgroundColor = [UIColor yellowColor];

[self.view addSubview:third];

// 設(shè)置第一個view在第一層;第二個在第三層;第三個在第四層;第四個在第二層

first.layer.zPosition = 1;? // red

second.layer.zPosition = 3; // green

third.layer.zPosition = 2;? // hello


最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容