0
A
回答
1
你可以使用这个滚动视图代表。例如,
func scrollViewDidScroll(_ scrollView: UIScrollView) {
if scrollView.panGestureRecognizer.translation(in: scrollView.superview).y > 0 {
//scrolling downwards
if scrollView.contentOffset.y < 0 {
//this means we are at top of the scrollView
changeSectionHeight(with scrollView.contentOffset.y, hide:false)
}
}
else {
//we are scrolling upward
changeSectionHeight(with scrollView.contentOffset.y, hide:true)
}
}
这是用户如何滚动向下或向上滚动的方式。现在基于此,我们可以隐藏或显示顶部部分(通过更改高度约束)。
//make IBoutlet for the top section height constraint
@IBOutlet weak var topSectionHeightConstraint: NSLayoutConstraint!
func changeSectionHeight(with offset:CGFloat, hide:Bool) {
let requiredHeight: CGFloat = hide ? 0.0 : 160.0 //let say when you want to hide the height is 0.0 and when you want to show it its 160.0
//If you want animation when showing and hiding use animate if not then simply change the constant for the constraint
if hide {
if (holderViewHeightConstraint.constant - offset) > requiredHeight {
UIView.animate(withDuration: 0.3, animations: {
self. topSectionHeightConstraint.constant -= offset
})
}
else {
UIView.animate(withDuration: 0.3, animations: {
self. topSectionHeightConstraint.constant = requiredHeight
})
}
}
else {
if (holderViewHeightConstraint.constant - offset) < requiredHeight {
UIView.animate(withDuration: 0.3, animations: {
self. topSectionHeightConstraint.constant -= offset
})
}
else {
UIView.animate(withDuration: 0.3, animations: {
self. topSectionHeightConstraint.constant = requiredHeight
})
}
}
}
相关问题
- 1. 向下滚动时隐藏导航
- 2. 滚动时隐藏bootstrap导航栏
- 3. 滚动时隐藏导航栏
- 4. 向下滚动时隐藏顶部栏
- 5. 如何隐藏/显示导航栏,当用户滚动向上/向下
- 6. 当用户滚动时隐藏在导航栏下的内容
- 7. 隐藏导航向下滚动并仅在移动时向上滚动
- 8. 如何在滚动时隐藏导航栏
- 9. 在隐藏状态栏及其背景时滚动时隐藏导航栏
- 10. 如何隐藏导航栏?
- 11. 如何隐藏导航栏?
- 12. 显示滚动向下滚动和隐藏向下滚动时
- 13. 修复导航栏上向下滚动
- 14. 隐藏导航栏
- 15. 隐藏导航栏?
- 16. 隐藏导航栏
- 17. 隐藏导航栏
- 18. 单击导航链接时如何向下滚动到导航栏
- 19. 折叠工具栏向上滚动时隐藏并向下滚动时显示
- 20. 如何让我的导航栏在滚动时上下滚动?
- 21. 滚动时隐藏和显示导航栏
- 22. 页面菜单,滚动时隐藏导航栏
- 23. 当滚动uiwebview时隐藏并显示导航栏
- 24. 以下导航栏时,滚动的jQuery
- 25. 水平滚动垂直导航栏 - 滚动时导航栏下的内容
- 26. 的UIButton是向上移动时,导航栏隐藏
- 27. 的UIButton向上滑动时,导航栏隐藏
- 28. 由于滚动量导致iOS隐藏导航栏
- 29. 将导航栏移动到顶部,同时向下滚动HTML
- 30. 当页面向下滚动时,导航栏移动到顶部
如何在代码中更改高度约束? – Riccardo
增加了改变高度的功能。没有测试,但你可以调整你想要的结果。 – kathayatnk