固体解决方案基于子视图灵活调整UIView大小 [英] Solid solution to flexibly resize UIView based on subviews

查看:108
本文介绍了固体解决方案基于子视图灵活调整UIView大小的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

想象你有四个左右的意见,所有宽度100,不同的高度。你有一个封装视图W,它拥有所有。

Imagine you have four or so views, all width 100, different heights. You have a wrapper view W which holds them all.

A   |
B   |  W
C   |
D   |

小视图的高度可以改变。当时你想让它们全部移动,浮动,适当地调整W的大小。

the heights of the small views can change. At that time you want them all to move, float, appropriately, and resize W.

现在,我只是要写几行代码来做到这一点。

Now, I was just about to write a few lines of code to do this.

所以..(1)你会有W找到所有的子视图,并按照从上到下的顺序列出。然后(2)每次有变化,你会(3)重新定位每个ABCD。每一个的位置是它上面的项目的高度的总和,以及(4)将W调整为所有高度的总和。

So .. (1) you'd have W find all the subviews and list them in order from top to bottom. Then (2) each time there is a change, you'd (3) reposition each of ABCD. the position of each one is the sum of the heights of the items above it, and (4) resize W to the sum of all heights.

现在这一切都很好,但 - 白痴重新创造了轮子!

Now that's all fine but -- idiots reinvent the wheel!

我在iOS中缺少明显的东西吗?是否已经有一个包大家用来做这一切的时间?还是内置的东西?

Am I missing something obvious in iOS? is there already a package everyone uses to do this all the time? Or something built in? What's the situation?

(请注意,对于我们的Android朋友来说,这是内置的!当然,任何web-html系统都会自动执行此操作。)

(Note that of course frustratingly, for our Android friends this is built in! And of course any web-html system does this automatically.)

这里是iOS视图的正确工程解决方案是什么?记录这是iOS7 +,没有老式的东西需要覆盖,如果它有所作为。干杯

What's the right engineering solution for iOS views here? For the record this is iOS7+ only, no old-fashioned stuffs need be covered, if it makes a difference. Cheers

推荐答案


(1)你会有W找到所有的子视图,从顶部
到底部。然后(2)每次有变化,你会(3)重新定位
每个ABCD。每一个的位置是
上面的项的高度的总和,以及(4)将W调整为所有高度的总和。

(1) you'd have W find all the subviews and list them in order from top to bottom. Then (2) each time there is a change, you'd (3) reposition each of ABCD. the position of each one is the sum of the heights of the items above it, and (4) resize W to the sum of all heights.

您可以在Interface Builder中对整个过程使用约束,不需要代码。执行以下操作:

You can use constraints in Interface Builder for that whole process, no code required. Do this:


  • 将子视图A的宽度设置为100

  • 约束B,C和D匹配A的宽度

  • 在A和B,B和C以及C和D之间添加垂直间距约束以维持它们的相对位置

  • add W之间的垂直间距约束(以灰色显示的超级视图)和A

  • 在W和D之间添加垂直间距约束

  • W和视图A之间的尾随空格约束

  • set the width of subview A to 100
  • constrain B, C, and D to match A's width
  • add vertical spacing constraints between A and B, B and C, and C and D to maintain their relative position
  • add a vertical spacing constraint between W (the superview, shown in gray) and A
  • add a vertical spacing constraint between W and D
  • add leading and trailing space constraints between W and view A

您将得到类似于以下内容的结果:

You'll end up with something that looks like this:

Xcode中的约束编辑器不是完全直观的,但是一旦你了解了你可以和不能使用IB中的约束,以及当你需要使用代码来设置时, 约束。

The constraints editor in Xcode isn't completely intuitive, but it is easy to use once you understand what you can and can't do with constraints in IB and when you need to use code to set up the constraints.

这篇关于固体解决方案基于子视图灵活调整UIView大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆