[英]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.