如何实现折叠header?
How to implement a collapsing header?
我试图实现折叠式 UITableView Header,但现在我没有取得任何进展。 This 是我的故事板。我尝试使用 scrollViewDidScroll(scrollView: UIScrollView) 委托方法,但是在容器中嵌入的 table 视图中滚动时,位置根本没有改变。 heightConstraint 是容器 header 视图的高度。
Here 是我的 class 的完整源代码。感谢您的帮助!解决这个问题已经有一段时间了。
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
}
向上滚动时 header 容器应该 disappear/change 它的位置。
以下是如何处理 headerView
height
和 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
时,- 展开
如果您仍然遇到任何问题,请告诉我。
我试图实现折叠式 UITableView Header,但现在我没有取得任何进展。 This 是我的故事板。我尝试使用 scrollViewDidScroll(scrollView: UIScrollView) 委托方法,但是在容器中嵌入的 table 视图中滚动时,位置根本没有改变。 heightConstraint 是容器 header 视图的高度。 Here 是我的 class 的完整源代码。感谢您的帮助!解决这个问题已经有一段时间了。
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
}
向上滚动时 header 容器应该 disappear/change 它的位置。
以下是如何处理 headerView
height
和 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
时,如果您仍然遇到任何问题,请告诉我。