提问人:Rohan Dalmotra 提问时间:10/21/2023 更新时间:10/21/2023 访问量:34
滚动时我的 tableView 标头没有卡在顶部,我也在 tableView 顶部添加了一个 scrollView
My tableView Header is not getting stick on top while scrolling, I'm adding a scrollView too at top of tableView
问:
这是我的代码
private let parentScrollView = UIScrollView()
private let scrollViewContentView = UIView()
private let tableView = UITableView(frame: CGRect.zero, style: .plain)
MARK:设置滚动视图
extension ViewController{
private func setupParentScollView() {
self.view.addSubview(parentScrollView)
parentScrollView.delegate = self
parentScrollView.isScrollEnabled = false
}
private func addParentScollViewConstraints() {
parentScrollView.snp.makeConstraints { make in
make.left.top.right.bottom.equalTo(self.view.safeAreaLayoutGuide)
}
}
}
MARK:设置滚动视图的内容视图
extension ViewController{
private func setupScollViewContentView() {
self.parentScrollView.addSubview(scrollViewContentView)
}
private func addScollViewContentViewConstraints() {
scrollViewContentView.snp.makeConstraints { make in
make.edges.equalTo(parentScrollView) // Set constraints to cover the entire scrollView.
make.width.equalTo(parentScrollView)
}
}
}
MARK: - 设置 TabelView
extension ViewController{
private func setupTableView() {
scrollViewContentView.addSubview(tableView)
tableView.delegate = self
tableView.dataSource = self
tableView.separatorStyle = .none
tableView.showsVerticalScrollIndicator = false
tableView.bounces = false
tableView.contentInset = UIEdgeInsets(top: 0, left: 0, bottom: 100, right: 0)
print("this is from tableView",tableView.style)
if #available(iOS 15.0, *) {
tableView.sectionHeaderTopPadding = 0
}
}
func addTableViewConstraints() {
tableView.snp.makeConstraints { (make) in
make.top.equalTo(searchBottomPaddingView.snp.bottom).offset(100)
make.left.right.bottom.equalTo(scrollViewContentView)
make.height.equalTo(parentScrollView.snp.height)
}
}
}
extension ViewController: UIScrollViewDelegate {
func scrollViewDidScroll(_ scrollView: UIScrollView) {
if scrollView == tableView {
let offsetY = tableView.contentOffset.y
print("this is from scrollView", offsetY, parentScrollView.contentOffset.x)
if offsetY > 0 && offsetY <= 120 {
// Scrolling down, move the parentScrollView up by 120 points
parentScrollView.setContentOffset(CGPoint(x: parentScrollView.contentOffset.x, y: 120), animated: true)
}
if offsetY == 0{
// Scrolling down, move the parentScrollView up by 120 points
parentScrollView.setContentOffset(CGPoint(x: parentScrollView.contentOffset.x, y: 0), animated: true)
}
}
**在这里,我不想滚动 tableView 的标题,但由于它位于 scrollView 的 contentView 内,因此它正在滚动 我一般想在滚动时获得视差效果,请帮帮我。 **
答: 暂无答案
评论