尝试设置较大视图的边界以匹配较小视图的边界。我刚掀起了一个简单的例子:
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