iOS 設置view的層級關系

1.設置某view到最上層

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

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

first.backgroundColor = [UIColor redColor];

[self.view addSubview:first];

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

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

second.backgroundColor = [UIColor greenColor];

[self.view addSubview:second];

// 設置第一個view到最上層

[self.view bringSubviewToFront:first];


2.設置某view到最下層

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

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

first.backgroundColor = [UIColor redColor];

[self.view addSubview:first];

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

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

second.backgroundColor = [UIColor greenColor];

[self.view addSubview:second];

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

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

third.backgroundColor = [UIColor yellowColor];

[self.view addSubview:third];

[self.view sendSubviewToBack:second];

// 設置第二個view到最下層

[self.view sendSubviewToBack:second];


3.設置某view到指定層

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

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

first.backgroundColor = [UIColor redColor];

[self.view addSubview:first];

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

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

second.backgroundColor = [UIColor greenColor];

[self.view addSubview:second];

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

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

third.backgroundColor = [UIColor yellowColor];

[self.view addSubview:third];

// 設置第一個view在第一層;第二個在第三層;第三個在第四層;第四個在第二層

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

second.layer.zPosition = 3; // green

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


最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容