繁体   English   中英

迅速如何显示除非到达底部后再向上滚动才能看到的页脚?

[英]swift how to display a footer that can't be seen unless scrolled up further after reaching bottom?

如果用户到达表格视图的底部但仍在向上滚动,我想向用户显示“全部”消息,就像聊天底部显示的“您最新”消息松弛一样。 但是, tableFooterView可以在表视图的最底部看到,并且不会被隐藏。 应该怎么做?

我使用以下解决方案:

let test=UILabel(frame: CGRect(x: 0,y: tableView.contentSize.height+180, width: tableView.frame.width, height: 50))
test.text="That's all"
view.insertSubview(test, belowSubview: tableView)

添加页脚视图将不起作用,因为随后表视图将调整其contentSize以显示它。

您可以将子视图直接添加到UITableView,将其框架的origin.y设置为大于contextSize.y。 每当您添加或删除行,添加或删除节,重新加载表时,都必须重新调整视图。

我遇到了同样的问题,上述解决方案对我不起作用。

我最终使用了自动布局约束。 最初,页脚视图的底部锚点设置为更大的常数,以使其不可见

然后使用滚动视图委托的开始和结束拖动方法来显示和隐藏它

extension ViewController: UITableViewDelegate {
    func scrollViewWillBeginDragging(_ scrollView: UIScrollView) {
        guard let bottomAnchor = self.bottomAnchor else { return }
        guard scrollView.contentSize.height > scrollView.frame.size.height else { return }
        let heightOfInvisibleContent = (scrollView.contentSize.height - scrollView.frame.size.height)
        print("height of invisible content: \(heightOfInvisibleContent), offset: \(scrollView.contentOffset.y)")
        guard scrollView.contentOffset.y >= heightOfInvisibleContent else { return }
        bottomAnchor.constant = moveUpConstant
        UIView.animate(withDuration: 0.5) {
            self.view.layoutIfNeeded()
        }
    }

    func scrollViewDidEndDragging(_ scrollView: UIScrollView, willDecelerate decelerate: Bool) {
        guard let bottomAnchor = self.bottomAnchor else { return }
        bottomAnchor.constant = moveDownConstant
        UIView.animate(withDuration: 0.5) {
            self.view.layoutIfNeeded()
        }
    }
}

我在我的仓库https://github.com/ramjyroo/iOS-Expedition中共享了完整的示例项目(FooterMessage)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM