【发布时间】:2018-11-02 13:48:13
【问题描述】:
我原来的ViewController 只包含一个这样的滚动视图:
现在我也有了自己的xib文件(CheckBoxView),主要由一个按钮组成,看这个截图:
我动态创建一些UIStackViews 并将它们添加到ScrollView 在这些UIStackViews 中我添加了我的xib 文件的多个实例。
我想要实现的是,StackViews 只是垂直堆叠。在 StackViews 中,我的 xib 文件中的 UIViews 也应该垂直堆叠。
所以 xib 视图不在整个视图中。由于我使用的是多操作系统引擎,我无法提供 swift/obj-c 代码。但这是我的 Java 代码:
for (ItemConfiguration config : itemInstance.getConfigurations()) {
List<DLRadioButton> radioButtons = new ArrayList<DLRadioButton>();
UIStackView configView = UIStackView.alloc().initWithFrame(new CGRect(new CGPoint(0, barHeight), new CGSize(displayWidth, displayHeight - barHeight)));
configView.setAxis(UILayoutConstraintAxis.Vertical);
configView.setDistribution(UIStackViewDistribution.EqualSpacing);
configView.setAlignment(UIStackViewAlignment.Center);
configView.setSpacing(30);
for (ConfigurationOption option : config.getOptions()) {
UIView checkBox = instantiateFromNib("CheckBoxView");
for (UIView v : checkBox.subviews()) {
if (v instanceof DLRadioButton) {
((DLRadioButton) v).setTitleForState(option.getName(), UIControlState.Normal);
//((DLRadioButton) v).setIconSquare(true);
radioButtons.add((DLRadioButton) v);
}
}
configView.addArrangedSubview(checkBox);
}
// group radiobuttons
//groupRadioButtons(radioButtons);
configView.setTranslatesAutoresizingMaskIntoConstraints(false);
scrollView().addSubview(configView);
configView.centerXAnchor().constraintEqualToAnchor(scrollView().centerXAnchor()).setActive(true);
configView.centerYAnchor().constraintEqualToAnchor(scrollView().centerYAnchor()).setActive(true);
}
private UIView instantiateFromNib(String name) {
return (UIView) UINib.nibWithNibNameBundle(name, null).instantiateWithOwnerOptions(null, null).firstObject();
}
我需要如何设置对齐方式等以实现我想要的。它应该是这样的:
【问题讨论】:
-
你的代码中
barHeight的值是多少? -
@AleksandrMedvedev 这是
UIApplication.sharedApplication().statusBarFrame().size().height();大约 35 块类似的矿石。它只是顶部的状态栏高度。但是既然你提到它,这个UIStackView的大小不应该是静态的。它应该随着内容动态增长,因此如果内容过多,它会因为外部UIScrollView而变得可滚动 -
如果您包含一个演示项目,它将更快地解决
标签: ios xcode xib uistackview