自动布局故事板约束基于子视图内容右对齐

autolayout storyboard constraints right aligned based on subview content

我正在尝试设置以下布局(所有垂直居中,所以我忽略了那个方面):

|        Label          btn         View       |
|-10-|-----X-----|-10-|--30--|-10-|---Y---|-10-|
A    B           C    D      E    F       G    H

按钮宽度是固定的(30)。根据其他一些因素,我在 运行 时向 [view] 添加了不同的子视图。不同的子视图有不同的宽度。

我希望 Y 随添加的子视图的宽度缩放(最好相应地响应更改)。

我想 X 占用 space 的剩余部分,从而使整个集合填满超级视图的宽度。

~

我正在尝试弄清楚如何使用故事板工具来做到这一点(在 运行 时处理约束和框架会使这个问题变得不同,对我来说更容易)。

我目前的间距设置为:

子视图的布局如下:

|-10-|-fixed width button-|-10-|-fixed width button-|- (no constraint) -|

也尝试过:

|-10-|-fixed width button-|-10-|-fixed width button-|-10-|

(这导致子视图的大小正确,但是 [view] 只占用了 UILabel 内容大小后剩下的任何 space。)

我已经尝试更改视图的内容拥抱和阻力属性(认为这是关键),但我能实现的唯一结果是:

具体来说,我把View的content hugging和compression属性设置的比label的大,以为这样会造成内容的拥抱,结果Label缩小到宽度内容和视图占据了其余部分。我读过这个 Cocoa Autolayout: content hugging vs content compression resistance priority 但要么误解了它 and/or 必须在我的解决方案中考虑其他因素。

以防万一,标签 运行 分为两行(有时)。

来自@rdelmar 的一些提示(谢谢!)。我设法找到了一个我相当满意的解决方案。

基本上,看起来我无法在情节提要中执行此操作,子视图不会指示父视图,因为它是通过代码添加的。

所以我需要添加一些约束,并且至关重要(避免它与子视图对事物的原始理解冲突:

[subview setTranslatesAutoresizingMaskIntoConstraints:NO];

NSMutableArray * constraints = [NSMutableArray array];
[constraints addObject:[NSLayoutConstraint constraintWithItem:subview
                                                            attribute:NSLayoutAttributeLeft
                                                            relatedBy:NSLayoutRelationEqual
                                                               toItem:view
                                                            attribute:NSLayoutAttributeLeftMargin
                                                           multiplier:1.0
                                                             constant:0]];
[constraints addObject:[NSLayoutConstraint constraintWithItem:subview
                                                            attribute:NSLayoutAttributeRight
                                                            relatedBy:NSLayoutRelationEqual 
                                                               toItem:view
                                                            attribute:NSLayoutAttributeRightMargin
                                                           multiplier:1.0
                                                             constant:0]];
[view addConstraints:constraints];

如果有人能够解释这个答案,或者确实改进(例如,仅故事板),那么我认为世界会变得更美好。