我为 View Controller 实现了 2 个 Xib 文件(1 个 View ,1 个 CollectionView)。我想在滚动时显示/隐藏 View 。我可以隐藏 View (set height:0) ,但是当我想返回 (set height:50) 时,它不起作用。
提前致谢。
func scrollViewWillEndDragging(_ scrollView: UIScrollView, withVelocity velocity: CGPoint, targetContentOffset: UnsafeMutablePointer<CGPoint>) {
if scrollView.panGestureRecognizer.translation(in: scrollView).y < 0{
self.searchFieldView.heightAnchor.constraint(equalToConstant: 0).isActive = true
self.searchFieldView.clipsToBounds = true
//self.view.setNeedsLayout()
self.view.layoutIfNeeded()
}
else{
self.searchFieldView.heightAnchor.constraint(equalToConstant: 50).isActive = true
//self.view.setNeedsLayout()
self.view.layoutIfNeeded()
}
}
最佳答案
从 xib 中引用 searchFieldView 的高度,并根据条件更改其常量值。
@IBOutlet weak var vwHeightConstraint: NSLayoutConstraint!
func scrollViewWillEndDragging(_ scrollView: UIScrollView, withVelocity velocity: CGPoint, targetContentOffset: UnsafeMutablePointer<CGPoint>) {
if scrollView.panGestureRecognizer.translation(in: scrollView).y < 0{
vwHeightConstraint.constant = 0
self.searchFieldView.clipsToBounds = true
//self.view.setNeedsLayout()
self.view.layoutIfNeeded()
}else{
vwHeightConstraint.constant = 50
//self.view.setNeedsLayout()
self.view.layoutIfNeeded()
}
}
希望它能奏效。试试这个吧
关于ios - 以编程方式快速更改 Xib 高度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54303885/