UIView *parentView = [[UIView alloc]initWithFrame:[CGRectMake(0,0,100,100)]];
UIView *childView = [[UIView alloc]initWithFrame:[CGRectMake(0,120,100,100)]];
[parentView addSubview:childView];
上面说明childView的代码在parentView绑定范围之外的位置添加到parentView中。无论如何,我可以调整parentView的大小,以便childView不再超出范围吗?我想动态地做到这一点。有没有可用的自动功能,或者我必须通过计算总体大小来手动执行。
另一个问题是,即使我设置了[parentView setClipsToBounds:NO],在调整parentView的大小以适合childView之前,似乎childView的添加位置是parentView的(0,100)而不是(0,120)。
最佳答案
您可以根据子视图框架调整父视图的大小
UIView *parentView = [[UIView alloc]initWithFrame:[CGRectMake(0,0,100,100)]];
UIView *childView = [[UIView alloc]initWithFrame:[CGRectMake(0,0,0,120)]];
[parentView addSubview:childView];
parentView.frame = CGRectMake(0,0,childView.frame.size.width,childView.frame.size.height);