简体   繁体   English

如何让superview适合自动布局的最大内在大小子视图

[英]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: 注意事项:

  • Can't use stack view 无法使用堆栈视图
  • subviews have dynamic widths. 子视图具有动态宽度。 So in the example any row could be the widest. 因此在示例中,任何行都可以是最宽的。

Okay here's how I got it working. 好的,这就是我如何运作的。 Not sure if this is the best way. 不确定这是不是最好的方法。

  1. Set all the trailing constraints to >= 将所有尾随约束设置为>=
  2. Set a width on the superview 在superview上设置宽度
  3. create an outlet for the superview width constraint 为superview宽度约束创建一个出口
  4. In your ViewController, override viewDidLayoutSubviews() 在ViewController中,覆盖viewDidLayoutSubviews()
    1. Calculate the largest width out of all your subviews 计算所有子视图中的最大宽度
    2. add your margins to that width 将您的边距添加到该宽度
    3. set the superview width constant to the max that was calculated 将superview width常量设置为计算的最大值

Follow the below steps, 按照以下步骤,

  1. Add a view (say view A) to the main view. 将视图(例如视图A)添加到主视图中。
  2. Add all three labels (say label 1, label 2, label 3)to the view A as sub views. 将所有三个标签(例如标签1,标签2,标签3)添加到视图A作为子视图。
  3. Pin View A to its super view to all four sides. 将A视图定位到四面的超视图。 Change constraints at the top,bottom,left and right as greater than equal to (>=)10 where priority is set as required(because to avoid view A extending out of the screen if contents extend). 更改顶部,底部,左侧和右侧的约束大于等于(> =)10,其中根据需要设置优先级(因为如果内容扩展,则避免视图A伸出屏幕)。
  4. Add constraints to label 1, that is pin it to top,right and left of View A by constant is equal to 0. And pin bottom of label 1 to label 2 top is equal to 10 where constraint priority is set as required. 向标签1添加约束,即将其固定到视图A的顶部,右侧和左侧,常数等于0.标签1到标签2顶部的引脚底部等于10,其中约束优先级根据需要设置。
  5. Follow same steps to label 2 as well. 按照相同的步骤标记2。
  6. Follow same steps to label 3. ie, pin it to bottom of label 2 and right, left of view A. Pin it to bottom of view A, where bottom constraint should be equal 0 where priority is set as low. 按照相同的步骤标记3.即,将其固定到标签2的底部和视图A的左侧。将其固定到视图A的底部,其中底部约束应该等于0,其中优先级设置为低。

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添加以下约束,

  • ViewA center X and Y to SuperView center X and Y (=0 @1000) ViewA中心X和Y到SuperView中心X和Y(= 0 @ 1000)
  • ViewA leading and SuperView leading (>=10 @1000) ViewA领先和SuperView领先(> = 10 @ 1000)
  • ViewA trailing and SupreView trailing (>=10 @1000) ViewA尾随和SupreView尾随(> = 10 @ 1000)
  • ViewA Top and SuperView Top (>=10 @1000) ViewA Top和SuperView Top(> = 10 @ 1000)
  • ViewA Bottom and Bottom trailing (>=10 @1000) ViewA底部和底部尾随(> = 10 @ 1000)
  • Image1 to superview Leading,top (=0 @1000) Image1到superview前导,顶部(= 0 @ 1000)
  • Image1 to superview trailing (>=0 @1000) Image1到superview尾随(> = 0 @ 1000)
  • Image1 to Image2 Bottom (=10 @1000) Image1到Image2 Bottom(= 10 @ 1000)
  • Image2 to superview Leading (=0 @1000) Image2到superview领先(= 0 @ 1000)
  • Image2 to superview trailing (>=0 @1000) Image2到superview尾随(> = 0 @ 1000)
  • Image2 to Image3 Bottom (=10 @1000) Image2到Image3 Bottom(= 10 @ 1000)
  • Image3 to superview Leading (=0 @1000) Image3到superview领先(= 0 @ 1000)
  • Image3 to superview trailing (>=0 @1000) Image3到superview尾随(> = 0 @ 1000)
  • Image3 to superView Bottom (=0 @999) Image3到superView Bottom(= 0 @ 999)

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM