[英]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
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.