简体   繁体   English

如何创建动态弹出的子视图并扩展其超视图大小。 (在代码中)

[英]How do I create a SubView that pops up dynamically and expands it's superview size. (in code)

So I currently have a UIView subclass called: " InputView " with two subviews, a UILabel and a UITextField . 所以我目前有一个UIView子类叫做:“ InputView ”有两个子视图,一个UILabel和一个UITextField The way i've set it up is I can change the label and text field placeholder so that this InputView can serve as a modular component for any input field in my app (email, password, etc.) All I have to do is create an empty UIView in my storyboard, set it's class as InputView , and have the rest be done programmatically. 我设置的方式是我可以更改标签和文本字段占位符,以便此InputView可以作为我的应用程序中任何输入字段的模块化组件(电子邮件,密码等)。我所要做的就是创建我的故事板中的空UIView ,将它的类设置为InputView ,其余的以编程方式完成。

Now, I'm running into an issue. 现在,我遇到了一个问题。 The InputView 's are plain UIView 's whose constraints are set in the storyboard, but I want to add a subview (a table) that appears/disappears dynamically whenever a user is editing the UITextField . InputView是普通的UIView ,它的约束是在故事板中设置的,但我想添加一个子视图(一个表),当用户编辑UITextField时,它会动态显示/消失。 Every time the table appears, the InputView expands in height, pushes everything below it down, and then disappears very neatly afterward. 每次表格出现时, InputView在高度上展开,将其下方的所有内容向下推,然后InputView消失。

I have no idea how to do that. 我不知道该怎么做。 Here is what I've thought of doing: 这是我想到的:

Idea 1: Place the table in the view, add all necessary constraints to it and hope InputView expands by itself. 想法1:将表放在视图中,为其添加所有必要的约束,并希望InputView自行扩展。 This lead to the table being outside the frame of the InputView , and therefore unresponsive to user touches for some reason 这导致表格位于InputView的框架InputView ,因此由于某种原因无法响应用户触摸

Idea 2: Change the bounds of the InputView and expand it's height by the table's height. 想法2:更改InputView的边界,并将其高度扩展为表格的高度。 This doesn't work because the constraints are set in the storyboard and I don't have access to them programmatically 这不起作用,因为约束是在故事板中设置的,我无法以编程方式访问它们

Idea 3: After almost 5 hours of research and failed attempts, go to StackOverflow and see if someone has an idea how to fix it. 想法3:经过近5个小时的研究和尝试失败后,转到StackOverflow,看看是否有人知道如何修复它。 (Thanks!) (谢谢!)

You can set things up so that your storyboard constraints can be changed programmatically. 您可以进行设置,以便可以通过编程方式更改故事板约束。

1) In the storyboard, set a height constraint for inputView and set it to the value that makes sense for when the table is not visible -- probably around 30. (You have probably already done this.) 1)在故事板中,为inputView设置一个高度约束,并将其设置为当表不可见时有意义的值 - 可能在30左右。(您可能已经这样做了。)

2) Then create an outlet to your code by control-dragging that height constraint to the file that corresponds to the view controller that has inputView in it. 2)然后通过控制将该高度约束拖动到与其中包含inputView的视图控制器对应的文件,为代码创建一个插座。 A dialog box will prompt you to give the outlet a name in your code. 对话框将提示您在代码中为插座指定名称。 Call it something like inputViewHeightConstraint . 称之为inputViewHeightConstraint

3) Then in your code, when you want to make your inputView have a bigger height, set inputViewHeightConstraint.constant to a new value. 3)然后在您的代码中,当您想使inputView具有更大的高度时,将inputViewHeightConstraint.constant设置为新值。 Note you need to refer to .constant in order to set the new height of the constraint. 请注意,您需要引用.constant以设置约束的新高度。

When you first set your height constraint for inputView, be sure to make it an "equals" constraint (eg, its height equals 30) rather than a "greater than or equals" constraint. 首次为inputView设置高度约束时,请确保使其为“等于”约束(例如,其高度等于30),而不是“大于或等于”约束。 Even though the latter would make more sense since you'll be changing it to a greater value in your code, I've found there are bugs with that approach. 即使后者更有意义,因为你将在代码中将其更改为更大的价值,我发现这种方法存在缺陷。 Making it a straight "equals" constraint works more cleanly. 使它成为一个直接的“等于”约束更加干净。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 UIViewAutoresizingMask将子视图宽度扩展到超级视图的范围之外 - UIViewAutoresizingMask expands subview width beyond superview's bounds 如何指定间距约束作为超级视图尺寸的一部分? - How do I specify a spacing constraint as a proportion of the superview's size? 子视图的大小不等于其父视图 - Subview's size not equal to its superview 超级视图将子视图置于最前面是行不通的。 怎么做? - It's not working that superview bring the subview to front. how to do it? 如何快速将其子视图塑造为子视图? - How can I shape a subview to it's superview in swift? 如何将子视图大小调整传播到superview大小调整? - How do I propagate subview resizing to superview resizing? 如何根据超级视图调整子视图的大小和位置? - how to adjust size and location of subview according to superview? 如何使用自动布局对子视图进行编程,使其占据其父视图宽度的50%但保持不变的高度? - How can I use autolayout to program a subview to take up 50% of its superview's width but remain a constant height? 如何在目标c中将具有子视图大小的子视图初始化为子视图? - How to init subview into superview with the superview size , in objective c? 移动子视图时,如何在其超视图中将其子视图保留在同一位置? - When moving a subview around, how do I keep a subview of it in the same place in its superview?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM