我正在抓取下一个控制器视图的屏幕截图(作为UIView对象),并希望将该屏幕截图放在我前面控制器视图中的一个小矩形内(如预览)。将大型UIView对象放在较小的UIView对象中的最佳方法是什么?
这不起作用:
UIView *screenshot = .... // screenshot from the next controller's view
smallViewBox.contentMode = UIViewContentModeScaleAspectFit;
[smallViewBox addSubView:screenshot];
答案 0 :(得分:1)
您可以在其上设置缩放变换。
screenshot.transform = CGAffineTransformMakeScale(0.5, 0.5);
答案 1 :(得分:1)
尝试设置较大视图的边界以匹配较小视图的边界。我刚刚发了一个简单的例子:
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];
这将导致可见的较大视图的红色 - 覆盖绿色较小视图的背景。在这种情况下,大视图是小视图的子视图并占据整个区域。