【发布时间】:2017-04-03 17:19:46
【问题描述】:
谁能告诉我如何实现一个有弹性的 UITableView 页脚。
我希望我的页脚中的图像在用户“过度滚动”时拉伸/增加大小。 (如果用户在 UITableView 已经在底部时向下滚动)
当前代码:
private let tableFooterHeight: CGFloat = 300
private var footerCustomView = UIImageView()
func setupFooterView(){
// Footer view
self.footerCustomView = UIImageView(frame: CGRect.zero)
self.footerCustomView.backgroundColor = UIColor.brown
self.footerCustomView.image = UIImage(named: "image")
self.view.addSubview(self.footerCustomView)
// self.view.bringSubview(toFront: self.tableView)
self.tableView.contentInset = UIEdgeInsets(top: 0, left: 0, bottom: self.tableFooterHeight, right: 0)
self.footerCustomView.translatesAutoresizingMaskIntoConstraints = false
let leadingConstraint = NSLayoutConstraint(item: self.footerCustomView,
attribute: .leading,
relatedBy: .equal,
toItem: self.tableView,
attribute: .leading,
multiplier: 1,
constant: 0)
let trailingConstraint = NSLayoutConstraint(item: self.footerCustomView,
attribute: .trailing,
relatedBy: .equal,
toItem: self.tableView,
attribute: .trailing,
multiplier: 1,
constant: 0)
let bottomConstraint = NSLayoutConstraint(item: self.footerCustomView,
attribute: .bottom,
relatedBy: .equal,
toItem: self.tableView,
attribute: .bottom,
multiplier: 1,
constant: 0)
let heightConstraint = NSLayoutConstraint(item: self.footerCustomView,
attribute: .height,
relatedBy: .equal,
toItem: nil,
attribute: .height,
multiplier: 1,
constant: self.tableFooterHeight)
self.view.addConstraints([bottomConstraint, trailingConstraint, heightConstraint, leadingConstraint])
}
func scrollViewDidScroll(_ scrollView: UIScrollView) {
self.updateFooterView()
}
func updateFooterView() {
if self.tableView.bounds.origin.y > 1559 { // 1559 is end of tableView
let diff = self.tableView.contentOffset.y - 1559
self.footerCustomView.frame.size.height = self.tableFooterHeight + diff
}
}
谢谢!
【问题讨论】:
-
您应该将底部的 contentInset 添加到表格视图中,并在表格视图的超级视图底部添加一个图像视图。确保它被添加到表格视图的后面。并在滚动视图委托中,scrollViewDidScroll,调整图像视图框架的过度滚动量。
-
感谢您的意见!我在上面的问题中更新了我的代码。 tableFooterView 正在向下增长。我试图在 tableviews 底部和 footerCustomView 之间添加底部约束,但没有运气。此外,当我调用 self.view.bringSubview(toFront: self.tableView) 时,我的 customView 不可见,并且被我最后一个单元格背景颜色覆盖。您有什么想法吗?我是否按照您的描述实现了代码?
标签: ios swift uitableview swift3 ios-animations