如何在 Swift 中隐藏元素及其 Space
How to Hide an Element and Its Space in Swift
我想隐藏一个元素(在我的例子中是数据选择器)并隐藏它的 space。所以我用动画试了一下:
@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
但它不起作用。
提前致谢。
使用 "hidden" property on views in Objective-C/Swift does as a matter of fact not "collapse" the space the view is taking (as opposed to "View.GONE" in Android).
相反,您应该使用自动布局和高度约束。
在您的 Xib/Storyboard 文件中创建高度限制。给它想要的高度。为该约束创建一个 IBOutlet(ctrl-从约束列表中的约束拖动到您的源文件),然后您可以编写此方法
Swift解法
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];
}
}
免责声明:我没有测试也没有编译上面的代码,但它会让你知道如何实现它。
重要:上面的代码假定您在 storyboard/nib.
中为选择器视图的高度约束赋予了一个大于 0 的值
这是一个老问题,但有另一个选项可用于较新的 iOS 版本:
如果您的布局允许,并且您的目标是 iOS9 或更高版本,您可以将视图安排在 UIStackView
作为容器。堆栈视图的子项在隐藏时会折叠,即不再占用任何 space.
Dynamically Changing the Stack View’s Content
The stack view automatically updates its layout whenever views are added, removed or inserted into the arrangedSubviews array, or whenever one of the arranged subviews’s isHidden property changes.
(https://developer.apple.com/documentation/uikit/uistackview#overview)
最好的方法是在堆栈中添加视图,隐藏一个视图时,堆栈视图的高度将自动调整。但是,如果视图不在堆栈视图中,则只需执行以下操作。
1- 隐藏视图。
2- 为视图分配高度约束,使视图的高度约束出口
@IBOutlet weak var myView: UIView!
@IBOutlet weak var myViewHeightConstraint: NSLayoutConstraint!
myView.isHidden = true
viewHeightConstraint.constant = 0
一个简单的方法是在隐藏视图时将高度设置为零(这也没有对高度进行 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()
块
如果你必须多次这样做,那么你可以通过在扩展中创建一个函数来将你的两行代码减少到一行。
我想隐藏一个元素(在我的例子中是数据选择器)并隐藏它的 space。所以我用动画试了一下:
@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
但它不起作用。
提前致谢。
使用 "hidden" property on views in Objective-C/Swift does as a matter of fact not "collapse" the space the view is taking (as opposed to "View.GONE" in Android).
相反,您应该使用自动布局和高度约束。
在您的 Xib/Storyboard 文件中创建高度限制。给它想要的高度。为该约束创建一个 IBOutlet(ctrl-从约束列表中的约束拖动到您的源文件),然后您可以编写此方法
Swift解法
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];
}
}
免责声明:我没有测试也没有编译上面的代码,但它会让你知道如何实现它。
重要:上面的代码假定您在 storyboard/nib.
中为选择器视图的高度约束赋予了一个大于 0 的值这是一个老问题,但有另一个选项可用于较新的 iOS 版本:
如果您的布局允许,并且您的目标是 iOS9 或更高版本,您可以将视图安排在 UIStackView
作为容器。堆栈视图的子项在隐藏时会折叠,即不再占用任何 space.
Dynamically Changing the Stack View’s Content
The stack view automatically updates its layout whenever views are added, removed or inserted into the arrangedSubviews array, or whenever one of the arranged subviews’s isHidden property changes.
(https://developer.apple.com/documentation/uikit/uistackview#overview)
最好的方法是在堆栈中添加视图,隐藏一个视图时,堆栈视图的高度将自动调整。但是,如果视图不在堆栈视图中,则只需执行以下操作。
1- 隐藏视图。
2- 为视图分配高度约束,使视图的高度约束出口
@IBOutlet weak var myView: UIView!
@IBOutlet weak var myViewHeightConstraint: NSLayoutConstraint!
myView.isHidden = true
viewHeightConstraint.constant = 0
一个简单的方法是在隐藏视图时将高度设置为零(这也没有对高度进行 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()
块
如果你必须多次这样做,那么你可以通过在扩展中创建一个函数来将你的两行代码减少到一行。