动态调整大小的嵌套UIStackView

时间:2018-11-29 23:08:09

标签: ios uitableview uistackview

我具有以下结构:

enter image description here

我在原型TableView单元格中有这个StackView。在此StackView中有两个视图。第一个是普通的“ UIView”,其内容在运行时之前已知。第二个是另一个StackView,我将在运行时填充多个UIViews

内部StackView从一开始就被设置为隐藏,并且仅在单击该单元格时才会显示。现在我的问题是,在将这些UIViews动态添加到StackView之后,我无法设法将单元格调整为正确的大小(高度)。

在更新单元格时,我为内部sizeToFit()和内部的每个layoutIfNeeded()调用StackViewUIView

这是我的代码,它填充内部UIStackView(在Java中,因为我使用的是multi-os-engine,但它与swift / objective-c非常相似)。

UIView parent = cell.orderDetailsView();
parent.subviews().makeObjectsPerformSelector(new SEL("removeFromSuperview"));

for (int i = 0; i < order.getOrderItems().size(); i++) {
    UserOrderItem item = order.getOrderItems().get(i);

    NSArray nibContents = nibBundle().loadNibNamedOwnerOptions("OrderListItem", this, null);
    UIView itemView = ((UIView) nibContents.lastObject()).subviews().get(0);
    parent.addSubview(itemView);

    // shortened code: here I set some uilabel content inside of the itemView

    if( i == 0) {
        NSLayoutConstraint c1 = NSLayoutConstraint.constraintWithItemAttributeRelatedByToItemAttributeMultiplierConstant(itemView, NSLayoutAttribute.Top, NSLayoutRelation.Equal, parent, NSLayoutAttribute.Top, 1, 5);
        parent.addConstraint(c1);
    }
    if (i == order.getOrderItems().size()-1) {
        // add last item
        NSArray nibContents2 = nibBundle().loadNibNamedOwnerOptions("OrderListTotalPrice", this, null);
        UIView totalPriceView = ((UIView) nibContents2.lastObject()).subviews().get(0);
        parent.addSubview(totalPriceView);
        ((UILabel) totalPriceView.subviews().get(0)).setText(DisplayUtils.getEuroString(order.getOrderFee()));
        ((UILabel) totalPriceView.subviews().get(1)).setText(DisplayUtils.getEuroString(order.getTotalPrice()));

        NSLayoutConstraint c1 = NSLayoutConstraint.constraintWithItemAttributeRelatedByToItemAttributeMultiplierConstant(totalPriceView, NSLayoutAttribute.Bottom, NSLayoutRelation.Equal, parent, NSLayoutAttribute.Bottom, 1, -5);
        parent.addConstraint(c1);

        NSLayoutConstraint c2 = NSLayoutConstraint.constraintWithItemAttributeRelatedByToItemAttributeMultiplierConstant(totalPriceView, NSLayoutAttribute.Leading, NSLayoutRelation.Equal, parent, NSLayoutAttribute.Leading, 1, 5);
        parent.addConstraint(c2);
        NSLayoutConstraint c3 = NSLayoutConstraint.constraintWithItemAttributeRelatedByToItemAttributeMultiplierConstant(totalPriceView, NSLayoutAttribute.Trailing, NSLayoutRelation.Equal, parent, NSLayoutAttribute.Trailing, 1, -5);
        parent.addConstraint(c3);
    }

    NSLayoutConstraint c2 = NSLayoutConstraint.constraintWithItemAttributeRelatedByToItemAttributeMultiplierConstant(itemView, NSLayoutAttribute.Leading, NSLayoutRelation.Equal, parent, NSLayoutAttribute.Leading, 1, 5);
    parent.addConstraint(c2);

    NSLayoutConstraint c3 = NSLayoutConstraint.constraintWithItemAttributeRelatedByToItemAttributeMultiplierConstant(itemView, NSLayoutAttribute.Trailing, NSLayoutRelation.Equal, parent, NSLayoutAttribute.Trailing, 1, -5);
    parent.addConstraint(c3);

    itemView.layoutIfNeeded();
    itemView.setNeedsDisplay();
}
// calling sizetofit and layoutifneeded for every subview here

PS:我的外部StackView设置为Fill equally,我认为这是造成问题的原因,但是当将其设置为成比例或仅填充内部StackView时,会在第一个元素(orderview)上方得到提示

内部StackView设置为fill proportionally

1 个答案:

答案 0 :(得分:0)

最后我发现了自己的错误:我需要使用.addArrangedSubview()而不是.addSubview()

这解决了整个问题。