我有一个简单的横向 UIStackView,里面堆叠了几个UIViews.我的目标是在视图之间创建可变间距.我很清楚我可以使用"spacing"属性在子视图之间创建恒定的空间.但是我的目标是创建可变空间.请注意,如果可能的话,我希望避免使用充当间隔物的隐形视图.
我想出的最好的方法是将我UIViews的内容包装起来UIStackView,并layoutMarginsRelativeArrangement = YES用来尊重内部堆栈的布局边距.我希望我可以做任何类似的事情UIView而不诉诸于这种丑陋的工作.这是我的示例代码:
// Create stack view
UIStackView *stackView = [[UIStackView alloc] init];
stackView.translatesAutoresizingMaskIntoConstraints = NO;
stackView.axis = UILayoutConstraintAxisHorizontal;
stackView.alignment = UIStackViewAlignmentCenter;
stackView.layoutMarginsRelativeArrangement = YES;
// Create subview
UIView *view1 = [[UIView alloc] init];
view1.translatesAutoresizingMaskIntoConstraints = NO;
// ... Add Auto Layout constraints for height / width
// ...
// I was hoping the layoutMargins would be respected, but they are not
view1.layoutMargins = UIEdgeInsetsMake(0, 25, 0, 0);
// …Run Code Online (Sandbox Code Playgroud)