嘗試設置較大視圖的邊界以匹配較小視圖的邊界。我剛掀起了一個簡單的例子:
UIView *largeView = [[UIView alloc] initWithFrame:CGRectMake(40, 40, 60, 60)];
largeView.backgroundColor = [UIColor redColor];
[self.view addSubview:largeView];
UIView *smallView = [[UIView alloc] initWithFrame:CGRectMake(50,50,40,40)];
smallView.backgroundColor = [UIColor greenColor];
[self.view addSubview:smallView];
largeView.bounds = smallView.bounds;
如果您註釋掉largeView.bounds = smallView.bounds綠色(小)框將是唯一一個可見的,因爲它正在草擬了在紅盒子控制器的視圖(在這種情況下,兩個視圖是兄弟姐妹)。爲了使大圖中較小的一個的子視圖,並將其限制在較小的區域,你可以這樣做:
UIView *largeView = [[UIView alloc] initWithFrame:CGRectMake(40, 40, 60, 60)];
largeView.backgroundColor = [UIColor redColor];
UIView *smallView = [[UIView alloc] initWithFrame:CGRectMake(50,50,40,40)];
smallView.backgroundColor = [UIColor greenColor];
[self.view addSubview:smallView];
largeView.frame = CGRectMake(0, 0, smallView.bounds.size.width, smallView.bounds.size.height);
[smallView addSubview:largeView];
這將導致更大的視圖的紅色可見 - 包括綠色小視圖的背景。在這種情況下,大視野是小視野的一個孩子,佔據了整個地區。
感謝您的詳細解答 – Nihat