tableView 滚动到顶部时出现大故障

时间:2021-07-17 14:56:12

标签: ios swift tableview scrollview visual-glitch

请帮助我并就我的问题提供建议。大家好! 我有 navigationBar(顶部白色区域)+ 自定义 HeaderView(橙色区域)+ TableView(绿色/黄色区域) 在向上滚动表格时,我的 customHeaderView 也应该滚动,在这里我将 topContaintAnchor 设置为该 customHeaderView 并在移动 tableView 的 offset.y 值时移动它。当当前 contentOffset 达到 customHeaderLabel 的大小并开始出现故障时,我无法滚动更多的问题。有时它工作得很好,但有时却没有。我在这里做错了什么?我该如何解决?

Here is a picture of my problem:

这是我的问题的简短视频:https://drive.google.com/file/d/16fSaWoMj2hdylSIxcrLeK3dEOS8UPf9t/view?usp=sharing

这是一个如何正确工作的视频: https://drive.google.com/file/d/1LmpLW08b7dy0OuTEEaCHHs8koKn1BYMM/view?usp=sharing

这是我的代码:

// For HeaderLabel
titleLabelTopConstraint = headerLabel.topAnchor.constraint(equalTo: safeAreaLayoutGuide.topAnchor)
headerLabel.leadingAnchor.constraint(equalTo: leadingAnchor, constant: 15),
headerLabel.trailingAnchor.constraint(equalTo: trailingAnchor, constant: -15),
headerLabel.bottomAnchor.constraint(equalTo: tableView.topAnchor)

// For TableView
tableView.topAnchor.constraint(equalTo: headerLabel.bottomAnchor),
tableView.leadingAnchor.constraint(equalTo: leadingAnchor),
tableView.trailingAnchor.constraint(equalTo: trailingAnchor),
tableView.bottomAnchor.constraint(equalTo: bottomAnchor),

// Delegate
open func scrollViewDidScroll(_ scrollView: UIScrollView) {
        
        guard UIScreen.type != .extraSmall else { return }

        let titleHeight = headerLabel.bounds.height

        if scrollView.contentOffset.y <= 0 {
            // Title is fully visible
            titleLabelTopConstraint.constant = 0
        } else if scrollView.contentOffset.y >= titleHeight {
            // Title is not visible at all
            titleLabelTopConstraint.constant = -titleHeight
        } else {
            // Title is not fully hidden or shown
            titleLabelTopConstraint.constant = -scrollView.contentOffset.y
        }
    }

0 个答案:

没有答案