简体   繁体   中英

Remove the unwanted space between button and line swift xcode

I am new to iPhone application development.

在此处输入图像描述

I am designing the app like this. in this design, there are two buttons below to delete button and below to those two button, there is the line. that you are seeing in this picture.

What i want is, i want to add the line below to the delete button when those two buttons are hidden by some condition. if those two buttons visible means, that line should come below to those two button. Not below to the delete button.

I have added the following code in cellForRowAt function of TableView. it is not working.

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

Any body can help me to solve this issue.

I have added following code to.

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


    return UITableViewAutomaticDimension
}

and

    dataTable.rowHeight = UITableViewAutomaticDimension
    dataTable.estimatedRowHeight = 100

You have to achieve this using some tricky way.Use Stack View Add Show,Hide buttons to one UIView.add all other components to another UIView.Add UIStackView to your custom cell and add constraints like this.

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

set stackView distribution to.Fill.

when you want to show/hide buttons, you can add/remove the arranged subView from tableview cell stackView.If you are show/hide buttons in cellForItemAt make sure remove all arranged subviews before add.

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 单元格结构

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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