向上滚动时使 UIScrollView 中的 UIView 粘在顶部 [英] make UIView in UIScrollView stick to the top when scrolled up
问题描述
因此,在 UITableView 中,当您有部分时,部分视图会粘在顶部,直到下一部分与它重叠,然后将其替换在顶部.我想有一个类似的效果,基本上我在我的 UIScrollView 中有一个 UIView,代表 UIView 的部分以及它何时到达顶部......我希望它留在那里而不是被抬高.我该怎么做呢?我认为这需要在 layoutSubviews 或 scrollViewDidScroll 中完成并对 UIVIew 进行操作..
So in a UITableView when you have sections the section view sticks to the top until the next section overlaps it and then it replaces it on top. I want to have a similar effect, where basically I have a UIView in my UIScrollView, representing the sections UIView and when it hits the top.. I want it to stay in there and not get carried up. How do I do this? I think this needs to be done in either layoutSubviews or scrollViewDidScroll and do a manipulation on the UIVIew..
推荐答案
要在 UIScrollView 中创建 UIView 在向上滚动时粘在顶部执行:
To create UIView in UIScrollView stick to the top when scrolled up do:
func createHeaderView(_ headerView: UIView?) {
self.headerView = headerView
headerViewInitialY = self.headerView.frame.origin.y
scrollView.addSubview(self.headerView)
scrollView.delegate = self
}
func scrollViewDidScroll(_ scrollView: UIScrollView) {
let headerFrame = headerView.frame
headerFrame.origin.y = CGFloat(max(headerViewInitialY, scrollView.contentOffset.y))
headerView.frame = headerFrame
}
这篇关于向上滚动时使 UIScrollView 中的 UIView 粘在顶部的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!