我有一个 TableView,我有一个位于 TableView 之上的 View ,因此当您在 TableView 上向下滚动时,始终会显示该 View 。我现在想要的是,当用户在 TableView 上向上滚动时,我希望俯 View 完全消失,根本看不到。现在,当用户向上滚动时, View 变成全白,但它仍然可见。有没有办法把它隐藏起来?如果它有助于俯 View 高度仅为 50.0
func scrollViewDidScroll(_ scrollView: UIScrollView) {
if (self.lastContentOffset > scrollView.contentOffset.y + 0) {
// TableView Moving up
View2.isHidden = true
View2.alpha = 0
}
}
这是一张图片,TopView 覆盖了 TableView 的一部分。
最佳答案
您可以使用 UITableHeaderView
实现这一点,如下所示。
这只是示例,您必须根据您的要求进行更改。
如下找到data
数组。
for i in 1...15 {
data.append("New Object \(i)")
}
现在像这样实现 UITableViewDataSource
方法,
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
return 44
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return data.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "CustomCell") as! CustomCell
cell.tmpValue.text = data[indexPath.row]
return cell
}
func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
return 50
}
func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
let headerView = UIView(frame: CGRect(x: 0, y: 0, width: tableView.frame.size.width, height: 50))
headerView.backgroundColor = UIColor.lightGray
let lblTemp = UILabel(frame: headerView.bounds)
lblTemp.text = "Header View"
headerView.addSubview(lblTemp)
return headerView
}
输出是这样的
此处 CustomCell
是您的自定义 TableView 单元格。
您甚至可以为 HeaderView 创建自定义 XIB,并根据您的要求进行相应操作。
仅供引用,要使 HeaderView 在滚动时向上滚动,您的 TableView 的样式应为 Grouped
。
希望它对你有用。
关于IOS View如何让它透明完全消失,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49376067/