Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/105.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 如何调整superview的大小,使其与Autolayout子视图的大小相匹配?_Ios_Interface Builder_Autolayout - Fatal编程技术网

Ios 如何调整superview的大小,使其与Autolayout子视图的大小相匹配?

Ios 如何调整superview的大小,使其与Autolayout子视图的大小相匹配?,ios,interface-builder,autolayout,Ios,Interface Builder,Autolayout,我有一个UIView(“superview”),它有两个uilabel作为子视图,在Interface Builder中设置。我已经启用了自动布局,以便在列表中一个接一个地正确分隔所有标签。这很有效 我现在要做的是使Superview垂直调整大小以匹配所有标签的高度,但我不太明白如何做到这一点 这是如何工作的?尝试使用UIView方法sizeToFit?尝试使用UIView方法sizeToFit?下面是一个使用可视化布局的示例。。。因此,忽略翻译自动调整大小GMaskintoConstraint

我有一个
UIView
(“superview”),它有两个
uilabel
作为子视图,在Interface Builder中设置。我已经启用了自动布局,以便在列表中一个接一个地正确分隔所有标签。这很有效

我现在要做的是使Superview垂直调整大小以匹配所有标签的高度,但我不太明白如何做到这一点


这是如何工作的?

尝试使用UIView方法
sizeToFit

尝试使用UIView方法
sizeToFit

下面是一个使用可视化布局的示例。。。因此,忽略翻译自动调整大小GMaskintoConstraints business

这取决于容器视图没有任何高度约束,并且似乎取决于视图之间的间距。系统将调整视图的大小以符合子视图的要求

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        self.backgroundColor = [UIColor redColor];

        [self setTranslatesAutoresizingMaskIntoConstraints:NO];

        _labelOne = [[UILabel alloc] init];
        _labelOne.text = @"Banana";
        _labelOne.backgroundColor = [UIColor blueColor];
        [_labelOne setTranslatesAutoresizingMaskIntoConstraints:NO];

        _labelTwo = [[UILabel alloc] init];
        _labelTwo.text = @"Dinosaur";
        _labelTwo.backgroundColor = [UIColor yellowColor];
        [_labelTwo setTranslatesAutoresizingMaskIntoConstraints:NO];

        [self addSubview:_labelOne];
        [self addSubview:_labelTwo];

        NSDictionary *views = NSDictionaryOfVariableBindings(_labelOne, _labelTwo);
        [self addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|[_labelOne]|" options:0 metrics:nil views:views]];
        [self addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|[_labelTwo]|" options:0 metrics:nil views:views]];
        [self addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|-10-[_labelOne(30)]-15-[_labelTwo(30)]-10-|" options:0 metrics:nil views:views]];
    }
    return self;
}

下面是一个使用可视化布局的示例。。。因此,忽略翻译自动调整大小GMaskintoConstraints business

这取决于容器视图没有任何高度约束,并且似乎取决于视图之间的间距。系统将调整视图的大小以符合子视图的要求

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        self.backgroundColor = [UIColor redColor];

        [self setTranslatesAutoresizingMaskIntoConstraints:NO];

        _labelOne = [[UILabel alloc] init];
        _labelOne.text = @"Banana";
        _labelOne.backgroundColor = [UIColor blueColor];
        [_labelOne setTranslatesAutoresizingMaskIntoConstraints:NO];

        _labelTwo = [[UILabel alloc] init];
        _labelTwo.text = @"Dinosaur";
        _labelTwo.backgroundColor = [UIColor yellowColor];
        [_labelTwo setTranslatesAutoresizingMaskIntoConstraints:NO];

        [self addSubview:_labelOne];
        [self addSubview:_labelTwo];

        NSDictionary *views = NSDictionaryOfVariableBindings(_labelOne, _labelTwo);
        [self addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|[_labelOne]|" options:0 metrics:nil views:views]];
        [self addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|[_labelTwo]|" options:0 metrics:nil views:views]];
        [self addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|-10-[_labelOne(30)]-15-[_labelTwo(30)]-10-|" options:0 metrics:nil views:views]];
    }
    return self;
}

我知道基本思想是让子视图的固有内容大小通过确保每个子视图的垂直维度中的内容压缩阻力和内容拥抱约束不会被您添加的更高优先级约束覆盖,从而驱动superview的高度

我知道最基本的想法是,通过确保每个子视图的垂直维度中的内容压缩阻力和内容包围约束不会被您添加的更高优先级约束覆盖,子视图的固有内容大小将驱动superview的高度

不知道,但我想是的。试试看,让我们知道:)不知道,但我想是的。试试看,让我们知道:)嗯,这很好,但问题是标签高度可能会改变,因为它们启用了多行(我想我应该在我的问题中提到这一点)。你可以改变固定的“30”高度,比如>=30,这样至少可以扩展视图。无法100%确定如何提前触发sizeToFit。嗯,这很好,但问题是标签高度可能会改变,因为它们启用了多行(我想我应该在问题中提到这一点)。你可以改变固定的“30”高度,比如>=30,这样至少可以扩展视图。无法100%确定如何提前触发sizeToFit。