【发布时间】:2014-07-11 12:29:56
【问题描述】:
使用新的 Autolayout 隐藏视图的最佳解决方案肯定是为视图创建高度约束,连接它并为其创建一个出口,并将 self.myViewHeightConstriant.constant 更改为 0。但假设视图包含其他一些视图,假设一个 imageView 和它下面的一些标签。现在,imageView 距离顶部 10px 并且具有顶部空间到具有 10px 值的 superview 约束。尝试使用常量 = 0 隐藏容器 UIView 在控制台中显示错误:
Unable to simultaneously satisfy constraints.
Probably at least one of the constraints in the following list is one you don't want.
Try this: (1) look at each constraint and try to figure out which you don't expect;
(2) find the code that added the unwanted constraint or constraints and fix it.
(Note: If you're seeing NSAutoresizingMaskLayoutConstraints that you don't
understand, refer to the documentation for the UIView property
translatesAutoresizingMaskIntoConstraints)
(
<NSLayoutConstraint:0xc7cedb0 V:[UIView:0xc7ce1e0(0)]>,
<NSLayoutConstraint:0xc7ceea0 V:[UIImageView:0xc7ce270]-(0)-| (Names: '|':UIView:0xc7ce1e0 )>,
<NSLayoutConstraint:0xc7cef30 V:|-(10)-[UIImageView:0xc7ce270] (Names: '|':UIView:0xc7ce1e0 )>
)
Will attempt to recover by breaking constraint
<NSLayoutConstraint:0xc7ceea0 V:[UIImageView:0xc7ce270]-(0)-| (Names: '|':UIView:0xc7ce1e0 )>
猜测问题是容器 UIView 的高度为 0,但 imageView 的顶部空间偏移量为 10px,并且 Autolayout 引擎不了解如何处理这种情况。试图为容器视图设置 clipSubviews 但这没有帮助。有什么想法吗?
更新 几个想法,为 imageView 创建一个出口 topSpaceToSuperView 约束并将其约束也设置为 0 看起来不是很吸引人。应该有比使用多个出口破坏代码更优雅的解决方案...
【问题讨论】:
标签: objective-c uiview hide autolayout