IOS View如何让它透明完全消失

标签 ios swift uiview uiscrollview tableview

我有一个 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 的一部分。

enter image description here

最佳答案

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

输出是这样的

enter image description here

此处 CustomCell 是您的自定义 TableView 单元格。

您甚至可以为 HeaderView 创建自定义 XIB,并根据您的要求进行相应操作。

仅供引用,要使 HeaderView 在滚动时向上滚动,您的 TableView 的样式应为 Grouped

希望它对你有用。

关于IOS View如何让它透明完全消失,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49376067/

相关文章:

ios - 我无法在 iOS 中安装 FireStore pod

ios - 如何在 iPhone 4/4S 屏幕的指定部分设置摄像头?

swift - UIScrollView contentOffset 在点击时重置

ios - 条形按钮项目未在 SWIFT 中显示

iphone - 在 iPhone 生产代码中使用常量 x/y 坐标是否安全

ios - 如何清除 iOS10 Swift 中的 UISearchBar 背景颜色?

javascript - jQuery Mobile 禁用页面内容垂直滚动

iOS 崩溃 libobjc.A.dylib objc_msgSend

ios - 根据方向调整弹出窗口大小

ios - 在所有其他 View 之上对 UIView 进行动画处理