这些是我的代码 //我的UIViews
@IBOutlet weak var UIVIewFirst: UIView!
@IBOutlet weak var UIViewSecond: UIView!
@IBOutlet weak var UIViewThird: UIView!
@IBOutlet weak var middleViewHeightConstraint: NSLayoutConstraint!
@IBOutlet weak var ViewThirdHeight: NSLayoutConstraint!
有一个按钮可以显示和隐藏视图;
@IBAction func infoClicked(sender: SSRadioButton) {
if UIViewSecond.hidden {
sender.selected = false
UIViewSecond.hidden = false
self.middleViewHeightConstraint.constant = 134
} else {
sender.selected = true
UIViewSecond.hidden = true
self.middleViewHeightConstraint.constant = 0
self.ViewThirdHeight.constant = 180
}
}
每个视图之间的垂直间隙为10.隐藏视图后,间隙变为20.但是我需要将它设置为第三视图和第二视图之间的10。即使我将第三个视图高度设置为任意数字也不会改变它的位置。任何人都可以建议为什么会发生这种情况?
答案 0 :(得分:1)
您没有告诉您的视图使用新约束更新视图,您必须调用此代码:
self.view.layoutIfNeeded()
答案 1 :(得分:1)
firstView
| gap = 10
secondView
| gap = 10
thirdView
删除secondView
后
firstView
| gap = 10
---------- height = 0
| gap = 10
thirdView
因此差距变为20
尝试在隐藏视图后以编程方式添加约束或减少任何一个间隙。
答案 2 :(得分:1)
答案 3 :(得分:1)
您需要在vertical spacing
或First-Second
视图之间采用Second-Third
约束的出口连接。此外,如果您只想隐藏/显示Second view
,则无需对Third View height constraint
进行任何更改。
说例如,我们选择vertical spacing between First and Second views
的出口,然后:
@IBOutlet weak var UIVIewFirst: UIView!
@IBOutlet weak var UIViewSecond: UIView!
@IBOutlet weak var middleViewHeightConstraint: NSLayoutConstraint!
@IBOutlet weak var verticalSpacingConstraint: NSLayoutConstraint!
@IBAction func infoClicked(sender: UIButton)
{
if UIViewSecond.hidden
{
sender.selected = false
UIViewSecond.hidden = false
self.middleViewHeightConstraint.constant = 134
self.verticalSpacingConstraint.constant = 10
}
else
{
sender.selected = true
UIViewSecond.hidden = true
self.middleViewHeightConstraint.constant = 0
self.verticalSpacingConstraint.constant = 0
}
}
以下是输出截图:
<强> 1。未选择按钮时
<强> 2。选择按钮时
答案 4 :(得分:-1)
尝试更改视图的框架而不是更改约束,也可以在进行任何更改后执行view.layoutIfNeeded。