【发布时间】:2016-04-26 23:39:47
【问题描述】:
我需要根据按下的按钮来更改 UI 元素,基本上它就像一个选项卡,但只是在我的视图控制器的一部分中。我需要显示/隐藏的元素是 UIView,里面有多个集合视图和标签。
我现在正在做的是在 IB 中定义两个对象,并且以编程方式定义两个对象,当我需要显示一个对象时,我只需将其中一个对象的宽度设置为 0,并为另一个对象设置一个具有整个宽度的新框架想。这种方法的问题在于,即使 UIView 是我想要的样子,里面的内容似乎也保留了以前的约束(居中),并且它们不会“适应”整个宽度。
当前代码:
var newFrame = self.view.frame
newFrame.origin.x = 0
newFrame.origin.y = self.blueView.frame.origin.y
newFrame.size.height = self.blueView.frame.size.height
if (showGreen){
self.greenView.frame = newFrame
self.greenView.hidden = false
self.blueView.frame.size.width = 0.0
self.blueView.hidden = true
}else{
self.blueView.frame = newFrame
self.blueView.hidden = false
self.greenView.frame.size.width = 0.0
self.greenView.hidden = true
}
在 IB 中,我将两个视图的约束设置为在它们和边距之间没有分隔。
我希望您能帮助我解决此问题,或通过其他方式实现所需的行为行为。
谢谢。
【问题讨论】:
标签: ios swift uiview constraints