swift 如何显示页脚到底部后必须向上滚动才能看到?
swift how to display a footer that can't be seen unless scrolled up further after reaching bottom?
如果用户到达 table 视图的底部但继续向上滚动,例如 "you're up to date" 消息松弛显示,我正在尝试向用户显示 "that's all" 消息在聊天的底部。但是,可以在 table 视图的最底部看到 tableFooterView
并且没有隐藏。应该怎么做?
添加页脚视图将不起作用,因为 table视图将调整其 contentSize 以显示它。
您可以直接在UITableView中添加一个子视图,设置其frame的origin.y大于contextSize.y。每当您添加或删除行,或添加或删除部分或重新加载 table 时,您都必须重新调整视图。
我使用这个解决方案:
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)
我遇到了同样的问题,上面的解决方案对我不起作用。
我最终使用了自动布局约束。最初,页脚视图的底部锚点被设置为更大的约束以使其不可见
然后使用滚动视图委托的开始和结束拖动方法来显示和隐藏它
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()
}
}
}
我已经在我的 repo 中分享了我的完整示例项目 (FooterMessage) https://github.com/ramjyroo/iOS-Expedition
如果用户到达 table 视图的底部但继续向上滚动,例如 "you're up to date" 消息松弛显示,我正在尝试向用户显示 "that's all" 消息在聊天的底部。但是,可以在 table 视图的最底部看到 tableFooterView
并且没有隐藏。应该怎么做?
添加页脚视图将不起作用,因为 table视图将调整其 contentSize 以显示它。
您可以直接在UITableView中添加一个子视图,设置其frame的origin.y大于contextSize.y。每当您添加或删除行,或添加或删除部分或重新加载 table 时,您都必须重新调整视图。
我使用这个解决方案:
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)
我遇到了同样的问题,上面的解决方案对我不起作用。
我最终使用了自动布局约束。最初,页脚视图的底部锚点被设置为更大的约束以使其不可见
然后使用滚动视图委托的开始和结束拖动方法来显示和隐藏它
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()
}
}
}
我已经在我的 repo 中分享了我的完整示例项目 (FooterMessage) https://github.com/ramjyroo/iOS-Expedition