【问题标题】:IOS View how to make it see-through and completely disapperIOS查看如何让它透明并完全消失
【发布时间】:2018-03-20 03:51:52
【问题描述】:

我有一个 TableView,并且我有一个位于 TableView 顶部的视图,因此当您在 TableView 上向下滚动时,该视图始终会显示。我现在想要的是,当用户在 TableView 上向上滚动时,我希望该俯视图完全消失并且根本看不到。现在,当用户向上滚动时,视图变为完全白色,但仍然可见。无论如何要隐藏它吗?如果有帮助,俯视图只有 50.0 高

 func scrollViewDidScroll(_ scrollView: UIScrollView) {
        if (self.lastContentOffset > scrollView.contentOffset.y + 0) {
            // TableView Moving up
          View2.isHidden = true
            View2.alpha = 0

        }

    }

这是一个 Image,TopView 覆盖了 TableView 的一部分。

【问题讨论】:

  • 你能发一张图片吗?
  • 是的,刚刚发布了一张图片
  • 将此视图作为headerview分配给tableview

标签: ios swift uiview uiscrollview tableview


【解决方案1】:

您可以使用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

希望它对你有用。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-07-03
    • 1970-01-01
    • 2014-02-23
    • 1970-01-01
    相关资源
    最近更新 更多