[英]How to insert UIButtons of Dynamic size into a UIStackView dynamically?
[英]Dynamically size nested UIStackView
我有以下结构:
我的原型TableView单元格中有此StackView。 在此StackView
内部有两个视图。 第一个是普通的“ UIView”,其内容在运行时之前已知。 第二个是另一个StackView
,我将与多个填补UIViews
上运行。
内部StackView
从一开始就设置为隐藏状态,并且仅在单击该单元格时才会显示。 我现在的问题是,我不能管理后动态添加这些得到的单元格大小合适(高度) UIViews
到StackView
。
更新单元格时,我为内部StackView
和内部的每个UIView
调用sizeToFit()
和layoutIfNeeded()
。
这是我的代码,它填充了内部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
。
最后,我发现了自己的错误:我需要使用.addArrangedSubview()
而不是.addSubview()
。
这解决了整个问题。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.