我有两个UIView
s,比如说containerView
和testView
。假设 的宽度testView
大于的宽度containerView
。在这种情况下,当我将testView
子视图添加到 时containerView
,我的问题就出现了。的框架testView
超出了containerView
. 这是我的代码 -
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
UIView *containerView = [[UIView alloc] initWithFrame:CGRectMake(60, 154, 200, 200)];
containerView.backgroundColor = [UIColor grayColor];
containerView.clipsToBounds = NO;
UIView *testView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 260, 100)];
testView.backgroundColor = [UIColor yellowColor];
testView.layer.borderWidth = 3;
testView.layer.borderColor = [[UIColor blackColor] CGColor];
testView.center = CGPointMake(containerView.frame.size.width / 2, containerView.frame.size.height / 2);
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(15, 25, 230, 50)];
label.text = @"This is a Label to test the size";
[testView addSubview:label];
[containerView addSubview:testView];
[self.view addSubview:containerView];
}
此代码的输出是 -
当 时containerView.clipsToBounds = YES;
,输出为 -
但我真正需要的是 - (编辑图片)
当我添加一个testView
宽度/高度大于它的 superview-containerView
时,应该调整(包括孩子)的框架testView
,使其完全适合它的 superview。
我欢迎你的想法..!