【问题标题】:Allign UIViews differnt when some UIViews are hidden?隐藏某些视图时对齐视图不同?
【发布时间】:2015-06-25 13:36:23
【问题描述】:
我有以下使用自动布局布局的视图。
--------
+
--------
加号图标在单元格上垂直居中。当出现第二个图标时,这应该会有所不同,如下图所示:
--------
+
X
--------
在这种情况下,两个图标应该垂直居中对齐,它们之间的间距为 5。
我在没有界面构建器或情节提要的代码中创建约束。
如何使用自动布局实现这一点?
【问题讨论】:
标签:
ios
objective-c
iphone
swift
uiview
【解决方案1】:
这很简单,但你需要一些代码
- 为约束创建出口,使您的“+”垂直对齐
- 当单元格上出现“X”时更改“常量”属性
例如
{
if (cell.secondView.hidden == true) {
// someConstraint - is your align constrain, which you can create throw IB
cell.someConstraint.constant = 0;
}
else
{
// note 'minus' - to move first view up
cell.someConstraint.constant = -cell.secondView.frame.height
}
}