[英]How to have superview fit to largest intrinsic size subview with autolayout
I have a column of multiple dynamic subviews with intrinsic sizes (images, labels, etc). 我有一列具有内在大小(图像,标签等)的多个动态子视图。 How can I have the superview fit the subviews in autolayout. 如何让superview适合autolayout中的子视图。
If the subviews were not dynamic, I would just constrain the largest subview's leading/trailing edge to the superview. 如果子视图不是动态的,我只会将最大的子视图的前导/后沿约束到superview。 Like so 像这样
I thought I'd be able to align to the leading edge and then have the trailing constraints be >= 8
, the logic being "The trailing edge can be greater than 8 if the subview is too narrow, but if the subview is wide expand the superview so there's at least a trailing edge of 8." 我以为我能够对齐前沿然后使尾随约束>= 8
,逻辑是“如果子视图太窄,后缘可能大于8,但如果子视图是宽扩展的话超级视图所以至少有一个8的后沿。“ But IB does not like that 但IB并不喜欢这样
Caveats: 注意事项:
Okay here's how I got it working. 好的,这就是我如何运作的。 Not sure if this is the best way. 不确定这是不是最好的方法。
>=
将所有尾随约束设置为>=
Follow the below steps, 按照以下步骤,
You can change constraints values based on your requirements. 您可以根据需要更改约束值。 It will work. 它会工作。 You can change the compression resistance priority to low whosoever label can shrink. 无论标签是否缩小,您都可以将抗压强度优先级更改为低。
UPDATE:For the below mentioned git link 更新:对于下面提到的git链接
For ViewA add following constraints, 对于ViewA添加以下约束,
Then select the ViewA and set update frames, there will be still red mark shown indicating Constraints are missing. 然后选择ViewA并设置更新帧,将显示仍然显示红色标记,表示缺少约束。 Still you can add the missing Constraint what xcode suggests but set it to remove at build time . 仍然可以添加缺少的约束xcode建议的内容,但将其设置为在构建时删除 。 Update the frames and run. 更新帧并运行。 Hope it works as per your requirement. 希望它按照您的要求工作。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.