我希望标题清楚。我想隐藏一个元素(在我的情况下是数据选择器),我也想隐藏他的空间。 所以我用动画尝试了这个:
@IBAction func showQnt(sender: AnyObject) {
if (self.pickerQnt.alpha == 0.0){
UIView.animateWithDuration(0.2, delay: 0.0, options: UIViewAnimationOptions.ShowHideTransitionViews, animations: {
self.pickerQnt.alpha = 1.0
}, completion: {
(finished: Bool) -> Void in
//var constH = NSLayoutConstraint(item: self.pickerQnt, attribute: NSLayoutAttribute.Height, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1, constant: 162)
//self.pickerQnt.addConstraint(constH)
})
} else {
UIView.animateWithDuration(0.2, delay: 0.0, options: UIViewAnimationOptions.ShowHideTransitionViews, animations: {
self.pickerQnt.alpha = 0.0
}, completion: {
(finished: Bool) -> Void in
// CHECK: ?!? constrain to set view height to 0 run time
//var constH = NSLayoutConstraint(item: self.pickerQnt, attribute: NSLayoutAttribute.Height, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1, constant: 0)
//self.pickerQnt.addConstraint(constH)
})
}
}
我也尝试过类似的事情:
self.pickerQnt.hidden = true
但不会工作。
提前致谢。
答案 0 :(得分:8)
在Objective-C / Swift中使用"hidden" property视图实际上不会“崩溃”视图所占用的空间(而不是"View.GONE" in Android)。
相反,您应该使用Autolayout和Height约束。
在Xib / Storyboard文件中创建高度约束。给它希望的高度。为该约束创建一个IBOutlet(从Constraints列表中的Constraint拖动到源文件),然后你可以编写这个方法
快速解决方案
var pickerHeightVisible: CGFloat!
...
...
func togglePickerViewVisibility(animated: Bool = true) {
if pickerHeightConstraint.constant != 0 {
pickerHeightVisible = pickerHeightConstraint.constant
pickerHeightConstraint.constant = 0
} else {
pickerHeightConstraint.constant = pickerHeightVisible
}
if animated {
UIView.animateWithDuration(0.2, animations: {
() -> Void in
self.view.layoutIfNeeded()
}, completion: nil)
} else {
view.layoutIfNeeded()
}
}
Objective-C解决方案:
@property (nonatomic, strong) CGFloat pickerHeightVisible;
...
...
- (void)togglePickerViewVisibility:(BOOL)animated {
if(pickerHeightConstraint.constant != 0) {
pickerHeightVisible = pickerHeightConstraint.constant;
pickerHeightConstraint.constant = 0;
} else {
pickerHeightConstraint.constant = pickerHeightVisible;
}
if(animated) {
[UIView animateWithDuration:0.2
animations:(void (^)(void))animations:^(void) {
[self.view layoutIfNeeded];
}];
} else {
[self.view layoutIfNeeded];
}
}
免责声明:我还没有测试过或编译过上面的代码,但它会让你知道如何实现它。
重要:上面的代码假设您在故事板/笔尖中为选择器视图的高度约束提供了大于0的值。
答案 1 :(得分:1)
最好的方法是在堆栈中添加视图,并且在隐藏一个视图时,堆栈视图的高度将自动调整。但是,如果视图不在堆栈视图中,请执行以下操作。
1-隐藏视图。
2-为视图分配高度约束,使视图的高度约束出口
HomeModule
答案 2 :(得分:1)
一种简单的方法是在隐藏视图时将高度设置为零(也无需设置IBOutlet
的高度限制)。
myView.isHidden = true
myView.heightAnchor.constraint(equalToConstant: CGFloat(0)).isActive = true
然后在取消隐藏时再次设置高度
myView.isHidden = false
myView.heightAnchor.constraint(equalToConstant: CGFloat(40)).isActive = true
要通过动画实现上述功能,只需将heightAnchor代码放在UIView.animate()
块中
如果必须执行多次,则可以通过在扩展中创建一个函数来将两行代码简化为一行。
答案 3 :(得分:0)
这是一个古老的问题,但是还有一个选项可用于更新的iOS版本:
如果您的布局允许,并且您定位到 iOS9或更高版本,则可以将视图安排在UIStackView
中作为容器。堆栈视图的子级在隐藏时会折叠,即不再占用任何空间。
动态更改堆栈视图的内容
无论何时将视图添加,移除或插入ArrangedSubviews数组中,或者每当已排列的子视图的isHidden属性之一更改时,堆栈视图都会自动更新其布局。
(https://developer.apple.com/documentation/uikit/uistackview#overview)