Динамический размер вложенного UIStackView - PullRequest
0 голосов
/ 30 ноября 2018

У меня есть следующая структура:

enter image description here

У меня есть этот StackView в моей прототипе ячейки TableView.Внутри этого StackView есть два вида.Первый - это обычный UIView, содержимое которого известно до времени выполнения.Вторым является еще один StackView, который я буду заполнять несколькими UIViews во время выполнения.

Внутренний StackView скрыт с самого начала и будет отображаться только при нажатии на ячейку.Теперь моя проблема в том, что я не могу получить ячейку нужного размера (высоты) после динамического добавления этих UIViews к StackView.

Я звоню sizeToFit() и layoutIfNeeded() для внутреннего StackView и каждого UIView внутри при обновлении ячейки.

Это мой код, который заполняет внутренний UIStackView (это в java, потому что я использую multi-os-engine, но он очень похож на swift / target-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 просто запрашивается над первым элементом (вид заказа).

Внутренний StackView установлен на fill proportionally.

1 Ответ

0 голосов
/ 07 января 2019

Наконец я нашел свою ошибку: мне нужно было использовать .addArrangedSubview() вместо .addSubview().

Это решило всю проблему.

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...