ios - 以编程方式快速更改 Xib 高度
问题描述
我将 2 个 Xib 文件实现到 View Controller(1 个 View,1 个 CollectionView)。我想在滚动时显示/隐藏视图。我可以隐藏视图 (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()
}
}
希望它会起作用。一旦尝试这个
推荐阅读
- angular - 量角器:.sendKeys() 将我的文本转换为大写
- ios - FIRAnalyticsConnector:为 Mac Catalyst 构建,但在为 iOS 模拟器构建的目标文件中链接
- c# - 如何为运行时排序创建 OrderBy 表达式树?
- python - 对字典中的重复索引求和并返回结果值
- react-native - SegmentedControlIOS 上的 fireEvent
- angular - ion-searchbar enter 触发自定义事件
- spring - 尽管是最新的,但未找到类 org.springframework.orm.hibernate5.localsessionfactorybean
- javascript - 尝试将数据从 Firebase 写入标题元素时,无法将属性“innerText”设置为 null
- css - 如何使用无单位的 CSS 变量,然后添加所需的单位?
- python - Leetcode 387 的时间复杂度