繁体   English   中英

删除按钮和线之间不需要的空间 swift xcode

[英]Remove the unwanted space between button and line swift xcode

我是 iPhone 应用程序开发的新手。

在此处输入图像描述

我正在设计这样的应用程序。 在这个设计中,下面有两个按钮用于删除按钮,在这两个按钮的下方,有线条。 你在这张照片中看到的。

我想要的是,当这两个按钮被某些条件隐藏时,我想将下面的行添加到删除按钮。 如果这两个按钮可见意味着,那条线应该在这两个按钮的下方。 不在删除按钮下方。

我在 TableView 的 cellForRowAt function 中添加了以下代码。 它不工作。

cell.line.frame = CGRect(x: 0, y:cell.btnDelete.frame.maxY, width: cell.line.frame.width , height: 1 ) 

任何机构都可以帮助我解决这个问题。

我添加了以下代码。

  func tableView(_ tableView: UITableView, estimatedHeightForRowAt indexPath: IndexPath) -> CGFloat {
      return UITableViewAutomaticDimension
}
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {


    return UITableViewAutomaticDimension
}

    dataTable.rowHeight = UITableViewAutomaticDimension
    dataTable.estimatedRowHeight = 100

您必须使用一些棘手的方法来实现这一点。使用堆栈视图添加显示,将按钮隐藏到一个 UIView。将所有其他组件添加到另一个 UIView。将 UIStackView 添加到您的自定义单元格并添加这样的约束。

 NSLayoutConstraint.activate([
            stackView.leadingAnchor.constraint(equalTo: leadingAnchor),
            stackView.trailingAnchor.constraint(equalTo: trailingAnchor),
            stackView.topAnchor.constraint(equalTo: topAnchor),
            stackView.bottomAnchor.constraint(equalTo: bottomAnchor),
 ])

将 stackView 分布设置为.Fill。

当您想要显示/隐藏按钮时,您可以从 tableview 单元格 stackView 添加/删除排列的子视图。如果您在cellForItemAt中显示/隐藏按钮,请确保在添加之前删除所有排列的子视图。

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

        cell.stackView.removeArrangedSubview(cell.viewOne)
        cell.stackView.removeArrangedSubview(cell.viewTwo)

        if isShowBuutons {
            cell.stackView.addArrangedSubview(cell.viewOne) //View with All other elements
            cell.stackView.addArrangedSubview(cell.viewTwo) //View with buttons
        }else{
            cell.stackView.addArrangedSubview(cell.viewOne) //View with All other elements
        }
    }

TableView 单元格结构

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM