如何移动uibutton来为小型门腾出空间?
我有一个snackbar,有时会出现在我的视图控制器上。但是当出现时,它会阻止已经存在的Uibutton。如何移动按钮以适应Snackbar,而无需硬编码移动按钮的位置?
谢谢!
I have a snackbar that appears at times on my view controller. But when it appears it blocks the UIButton that already exists. How do I shift the button to accommodate for the snackbar without hardcoding moving the position of the button?
Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以通过将按钮的底部约束取决于Snackbar容器的顶部约束来做到这一点(如果Snackbar从上方降下)。当什么都没发生时,Snackbar容器高度为零,并且在显示Snackbar时会展开。
但是,我会提出另一种方法。 Snackbars通常不会取代UI,我想用户“错过”按钮会感到沮丧,因为该应用显示Snackbar。另请参见材料设计指南这些事情如何工作。 (它们不是iOS的本地,因此据我所知,苹果没有任何文件)。
You could probably do this by having the button's bottom constraint depend on the snackbar container's top constraint (or vice versa, if the snackbar comes down from above). The snackbar container height is zero when nothing happens, and expands when a snackbar is displayed.
I would, however, suggest a different approach. Snackbars don't usually displace UI, and I guess it would be frustrating for users to "miss" the button because the app displays a snackbar. See also the Material Design Guidelines for how these things might work. (They aren't native to iOS, so Apple has no documentation as far as I know).
这可以通过为按钮的底部约束声明两个不同的nslayoutconstraints,一个基于安全区域(或最初相对于您的按钮定位),而另一个基于Snackbar的位置。然后,通过设置约束属性的属性 false 和
true
之间,实际使用哪个约束。我已经在此处确认这有效:
附加详细信息:按钮设置在Main.Storyboard中,除了相对于Snackbar的限制外,所有约束也是如此。我在代码示例的注释中包括了与示例相关的接口构建器值(按钮的底部约束)。
This can be done by declaring two different NSLayoutConstraints for your button's bottom constraint, one based on the safe area (or whatever your button is originally positioned relative to), and another based on the position of the snackbar. Then, toggle between which constraint is actually used by setting the respective
.isActive
properties of the constraints tofalse
andtrue
.I have confirmed that this works in the example here:
Additional details: The buttons were set up in Main.storyboard, as were all constraints except for the one which is relative to the snackbar. I included the Interface Builder values used for the one relevant to the example (button's bottom constraint) in the comments in the code sample.