简体   繁体   English

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

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

I'm trying to display a "that's all" message to the user if the user reaches the bottom of the table view but keeps scrolling up, like the "you're up to date" message slack displays at the bottom of the chat. 如果用户到达表格视图的底部但仍在向上滚动,我想向用户显示“全部”消息,就像聊天底部显示的“您最新”消息松弛一样。 However, the tableFooterView can be seen at the very bottom of the table view and isn't hidden. 但是, tableFooterView可以在表视图的最底部看到,并且不会被隐藏。 How should this be done? 应该怎么做?

I use this solution: 我使用以下解决方案:

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)

Adding a footerview won't work because then the tableview will adjust its contentSize to display it. 添加页脚视图将不起作用,因为随后表视图将调整其contentSize以显示它。

You can add a subview directly to the UITableView, set its frame's origin.y to be greater than the contextSize.y. 您可以将子视图直接添加到UITableView,将其框架的origin.y设置为大于contextSize.y。 Whenever you add or remove rows, or add or remove section section or reload the table you have to readjust the view. 每当您添加或删除行,添加或删除节,重新加载表时,都必须重新调整视图。

I faced the same problem and the above solution did not work for me. 我遇到了同样的问题,上述解决方案对我不起作用。

I ended up using auto layout constraints. 我最终使用了自动布局约束。 Initially the footer view's bottom anchor is set a bigger constrant to keep it invisible 最初,页脚视图的底部锚点设置为更大的常数,以使其不可见

Then used begin & end dragging methods of scroll view delegate to show & hide it 然后使用滚动视图委托的开始和结束拖动方法来显示和隐藏它

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()
        }
    }
}

I have shared my full sample project (FooterMessage) in my repo https://github.com/ramjyroo/iOS-Expedition 我在我的仓库https://github.com/ramjyroo/iOS-Expedition中共享了完整的示例项目(FooterMessage)

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

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