ios - 如何实现折叠标题?
问题描述
我试图实现一个折叠的 UITableView Header,但现在我没有得到任何进一步的。这是我的故事板。我尝试使用scrollViewDidScroll(scrollView: UIScrollView)委托方法,但在嵌入在容器中的表格视图中滚动时位置根本没有改变。heightConstraint是我的容器标题视图的高度 。这是我的课程的完整源代码。我很感激任何帮助!现在坐在这个问题上一段时间了。
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
self.heightConstraint.constant = self.maxHeaderHeight
}
func scrollViewDidScroll(_ scrollView: UIScrollView) {
let absoluteTop: CGFloat = 0
let absoluteBottom: CGFloat = scrollView.contentSize.height - scrollView.frame.size.height
let scrollDiff = scrollView.contentOffset.y - self.previousScrollOffset
let isScrollingDown = scrollDiff > 0 && scrollView.contentOffset.y > absoluteTop
let isScrollingUp = scrollDiff < 0 && scrollView.contentOffset.y < absoluteBottom
var newHeight = self.heightConstraint.constant
if isScrollingDown {
newHeight = max(self.minHeaderHeight, self.heightConstraint.constant - abs(scrollDiff))
} else if isScrollingUp {
newHeight = min(self.maxHeaderHeight, self.heightConstraint.constant + abs(scrollDiff))
}
if newHeight != self.heightConstraint.constant {
self.heightConstraint.constant = newHeight
}
self.previousScrollOffset = scrollView.contentOffset.y
}
向上滚动标题容器时应该消失/更改其位置。
解决方案
这是处理headerView
height
with 的方法tableView
scrolling
,
class VC: UIViewController {
@IBOutlet weak var heightConstraint: NSLayoutConstraint!
var lastContentOffset: CGFloat = 0.0
let maxHeaderHeight: CGFloat = 115.0
func scrollViewDidScroll(_ scrollView: UIScrollView) {
if (scrollView.contentOffset.y >= (scrollView.contentSize.height - scrollView.frame.size.height)) {
//Scrolled to bottom
UIView.animate(withDuration: 0.3) {
self.heightConstraint.constant = 0.0
self.view.layoutIfNeeded()
}
}
else if (scrollView.contentOffset.y < self.lastContentOffset || scrollView.contentOffset.y <= 0) && (self.heightConstraint.constant != self.maxHeaderHeight) {
//Scrolling up, scrolled to top
UIView.animate(withDuration: 0.3) {
self.heightConstraint.constant = self.maxHeaderHeight
self.view.layoutIfNeeded()
}
}
else if (scrollView.contentOffset.y > self.lastContentOffset) && self.heightConstraint.constant != 0.0 {
//Scrolling down
UIView.animate(withDuration: 0.3) {
self.heightConstraint.constant = 0.0
self.view.layoutIfNeeded()
}
}
self.lastContentOffset = scrollView.contentOffset.y
}
}
在上面的代码headerView
中,
- 崩溃时
tableView
_scrolled up
- 当
tableView
是时展开scrolled down
如果您仍然遇到任何问题,请告诉我。
推荐阅读
- entity-framework - 使用 .NET Core 处理和注入 DbContexts
- java - 向android中的URL列表发出多个get请求
- javascript - 无法让 RegEx 模式在使用 Javascript 的 MVC 5 View 中正常工作
- visual-studio-code - VSCode 中的 PlatformIO
- javascript - Canvas HTML - lineTo 和 Bezier 曲线之间的平滑度
- solr - 如何将两个文档作为一个文档插入到 solr
- sql - 如何根据具有多关系的最旧元素的属性编写 SQL 查询?
- python - 我可以有条件地改变一个列表吗?
- javascript - 有一个 if 语句不起作用的 each()
- r - 如何确保闪亮应用程序的用户在 R 中使用正确的包版本