自动版式与隐藏UIViews? [英] AutoLayout with hidden UIViews?

查看:130
本文介绍了自动版式与隐藏UIViews?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我觉得这是一个相当普遍的模式显示/隐藏 UIViews ,最常见的 UILabels ,视企业逻辑。我的问题是,什么是使用自动版式到隐藏的意见,如果他们的框架是0x0到回应的最好方式。这里是1-3特征的动态列表的一个例子。

I feel like it's a fairly common paradigm to show/hide UIViews, most often UILabels, depending on business logic. My question is, what is the best way using AutoLayout to respond to hidden views as if their frame was 0x0. Here is an example of a dynamic list of 1-3 features.

现在我有从按钮到最后一个标签,这显然不会滑起来的时候标签是隐藏的一个10px的顶部空间。截至目前我根据我多少标签显示创建了一个出口这种约束和修改的常数。这显然​​是一个有点哈克,因为我使用的是负常值在隐藏帧按下按钮了。这也是不好的,因为它没有被限制到实际的布局元素,只是基于已知的高度/其他元素的填充,明显对抗什么自动版式是专为鬼鬼祟祟的静态计算。

Right now I have a 10px top space from the button to the last label, which obviously won't slide up when the the label is hidden. As of right now I created an outlet to this constraint and modifying the constant depending on how many labels I'm displaying. This is obviously a bit hacky since I'm using negative constant values to push the button up over the hidden frames. It's also bad because it's not being constrained to actual layout elements, just sneaky static calculations based on known heights/padding of other elements, and obviously fighting against what AutoLayout was built for.

我可以明明只是创造新的限制取决于我的动态标签,但是这是一个很大的微观和大量的冗长的尝试只是崩溃一些空白。有没有更好的办法?更改帧大小0,0和让自动版式做它的事,没有操作的限制?彻底删除的看法?

I could obviously just create new constraints depending on my dynamic labels, but that's a lot of micromanaging and a lot of verbosity for trying to just collapse some whitespace. Are there better approaches? Changing frame size 0,0 and letting AutoLayout do its thing with no manipulation of constraints? Removing views completely?

老实说,虽然,刚刚从隐藏视图的情况下修改常量需要code的简单计算一行。重新创建新的约束与 constraintWithItem:属性:relatedBy:toItem:属性:事半功倍:常量:显得那么沉重。

Honestly though, just modifying the constant from context of the hidden view requires a single line of code with simple calculation. Recreating new constraints with constraintWithItem:attribute:relatedBy:toItem:attribute:multiplier:constant: seems so heavy.

推荐答案

我个人的preference为显示/隐藏的观点是用适当的宽度或高度约束创建一个IBOutlet。

My personal preference for showing/hiding views is to create an IBOutlet with the appropriate width or height constraint.

我再更新 0.f 来隐藏,或任何值应该是展现

I then update the constant value to 0.f to hide, or whatever the value should be to show.

此技术的最大优点是,相对制约将保持不变。例如,让我们说你有视图A和视图B用的 X 的的水平差距。当查看宽度设置为 0.f 然后查看B就向左移动来填补这一空间。

The big advantage of this technique is that relative constraints will be maintained. For example let's say you have view A and view B with a horizontal gap of x. When view A width constant is set to 0.f then view B will move left to fill that space.

有没有必要添加或删除约束,这是一个重量级的操作。只需更新约束的会做的伎俩。

There's no need to add or remove constraints, which is a heavyweight operation. Simply updating the constraint's constant will do the trick.

这篇关于自动版式与隐藏UIViews?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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