1

我有两个UIViews,比如说containerViewtestView。假设 的宽度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。

我欢迎你的想法..!

4

2 回答 2

4

执行此操作的现代方法是创建视图,向视图添加约束以建立与包含视图的关系,然后将该视图添加为子视图。

这样你就不会弄乱框架,并且会为你处理旋转调整大小。

编辑添加

这是一些示例代码,可以执行类似于您要求的操作

- (void)viewDidLoad {
    [super viewDidLoad];

    // Create the containing view
    UIView *greyView = [[UIView alloc] initWithFrame:CGRectZero];
    greyView.backgroundColor = [UIColor grayColor];
    greyView.translatesAutoresizingMaskIntoConstraints = NO;

    [self.view addSubview:greyView];

    // Create the label to go into the containing view
    UILabel *label = [[UILabel alloc] initWithFrame:CGRectZero];
    label.translatesAutoresizingMaskIntoConstraints = NO;
    label.text = @"Example Text";

    label.backgroundColor = [UIColor yellowColor];

    [greyView addSubview:label];

    NSDictionary *bindings = NSDictionaryOfVariableBindings(greyView, label);

    // Set the constraints for the greyView relative to it's superview - to cover completely
    [self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|[greyView]|" options:NSLayoutFormatDirectionLeadingToTrailing metrics:nil views:bindings]];
    [self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|[greyView]|" options:NSLayoutFormatDirectionLeadingToTrailing metrics:nil views:bindings]];

    // Set the constraints for the label to be pinned equally with padding
    [greyView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|-(30)-[label]-(30)-|" options:NSLayoutFormatDirectionLeadingToTrailing metrics:nil views:bindings]];
    [greyView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|-(30)-[label]-(30)-|" options:NSLayoutFormatDirectionLeadingToTrailing metrics:nil views:bindings]];

}

如果您想查看它的实际效果并自己调整约束,我已经上传了一个简单的示例项目,您可以使用它。

于 2013-10-07T11:51:18.657 回答
0

为什么将 testView 的宽度(黄色)设置为大于其容器的宽度,然后抱怨它变大了?你应该这样做

UIView *containerView = [[UIView alloc] initWithFrame:CGRectMake(60, 154, 200, 200)];
[...]
UIView *testView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 180, 100)]; // 180 not 260
[...]
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(15, 25, 150, 50)]; // 150 not 230

或使用建议的自动布局。

于 2013-10-08T09:49:13.877 回答