带有隐藏 UIView 的自动布局? [英] AutoLayout with hidden UIViews?

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

问题描述

我觉得显示/隐藏 UIViews 是一种相当常见的范例,最常见的是 UILabels,具体取决于业务逻辑.我的问题是,使用 AutoLayout 响应隐藏视图的最佳方式是什么,就好像它们的框架是 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 的顶部空间,当标签隐藏时显然不会向上滑动.截至目前,我为这个约束创建了一个出口,并根据我显示的标签数量来修改常量.这显然有点棘手,因为我使用负常数值将按钮向上推到隐藏帧上.这也很糟糕,因为它不受实际布局元素的限制,只是基于其他元素的已知高度/填充的偷偷摸摸的静态计算,并且显然与 AutoLayout 的构建目的作斗争.

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 并让 AutoLayout 在不操纵约束的情况下完成其工作?完全删除视图?

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?

老实说,仅仅从隐藏视图的上下文中修改常量就需要一行代码和简单的计算.用 constraintWithItem:attribute:relatedBy:toItem:attribute:multiplier:constant: 重新创建新约束似乎很繁重.

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.

2018 年 2 月编辑:使用 UIStackViews

推荐答案

UIStackView 可能是适用于 iOS 9+ 的方式.它不仅可以处理隐藏视图,如果设置正确,它还会移除额外的间距和边距.

UIStackView is probably the way to go for iOS 9+. Not only does it handle the hidden view, it will also remove additional spacing and margins if set up correctly.

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

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