继续在按钮单击的滚动视图中添加动态高度文本视图

时间:2018-04-09 06:17:33

标签: swift uiscrollview

我正在准备一个表单,我们必须在每次点击按钮时添加UI文本视图。此文本视图必须在键入文本时增加其高度,滚动视图应相应地增加其内容大小。 我已经尝试使用UITableView,滚动enabled = false并将文本视图放在单元格中,滚动启用= false。现在我正在尝试增加表格视图的内容大小以及滚动视图,并在每次单击按钮时添加新单元格,但无法执行此操作。有人能帮忙吗 ?

var notesArray = [String]()

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    if notesArray.count == 0{
        return 1
    }else{
        return notesArray.count
    }
}

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

    let cell : AddAssetCell = tableView.dequeueReusableCell(withIdentifier: "AddAssetCell", for: indexPath) as! AddAssetCell
    cell.contentView.isUserInteractionEnabled = true
    cell.txtView.delegate = self
    if notesArray.count > 0{
        cell.txtView.text = notesArray[indexPath.row]
    }
    return cell
}

func textViewDidChange(_ textView: UITextView) {

    let startHeight = textView.frame.size.height
    let calcHeight = textView.sizeThatFits(textView.frame.size).height 
    if startHeight != calcHeight {

        UIView.setAnimationsEnabled(false) // Disable animations
        self.tableViewNotes.beginUpdates()
        self.tableViewNotes.endUpdates()

        let scrollTo = self.tableViewNotes.contentSize.height - self.tableViewNotes.frame.size.height
        self.tableViewNotes.setContentOffset(CGPoint(x : 0, y : scrollTo), animated: false)
        contraintHeightTblViewNotes.constant = self.tableViewNotes.contentSize.height
        self.scrollViewAddAsset.contentSize.height = self.scrollViewAddAsset.contentSize.height + scrollTo
        UIView.setAnimationsEnabled(true)  // Re-enable animations.
    }
}

//MARK: - Text View Delegates
func textViewDidBeginEditing(_ textView: UITextView) {
    if textView.textColor == UIColor.lightGray {
        textView.text = nil
        textView.textColor = UIColor.black
    }
}
func textViewDidEndEditing(_ textView: UITextView) {
    if textView.text.isEmpty {
        textView.text = "Notes"
        textView.textColor = UIColor.lightGray
    }else{
        if notesArray.count > 0 {
            notesArray.insert(textView.text, at: notesArray.count - 1)
        }else{
            notesArray.append(textView.text)
        }
    }
}

@IBAction func btnAddMoreAction(_ sender: Any) 
{

    notesArray.append("")
    tableViewNotes.reloadData()
}

1 个答案:

答案 0 :(得分:0)

好的,让我们在UITableView中谈谈 Self-Sizing Cell ,如果您还不知道,这里有一个明确的教程https://www.raywenderlich.com/129059/self-sizing-table-view-cells

它让你的UITableView在摘要中自动增加它的单元格高度。所以下一个挑战是UITextViewDelegate,这是我最喜欢的解决方案,面对自我调整 UITextView 。:

func textViewDidBeginEditing(_ textView: UITextView) {
    if textView === txtDescription, textView.text == txtDescripttionPlaceHolder {
        textView.text = nil
        textView.selectedRange = NSMakeRange(0, 0)
    }
}

func textView(_ textView: UITextView, shouldChangeTextIn range: NSRange, replacementText text: String) -> Bool {
    let newLenght = textView.text.count + text.count - range.length
    if textView === txtDescription, textView.text == txtDescripttionPlaceHolder, newLenght > 0 {
        if text.count == 0 {
            return false
        }
        textView.text = nil
    }
    return true
}

func textViewDidChange(_ textView: UITextView) {
    if textView === txtDescription {
        resizeTxtDescription()
    }
}

func textViewDidEndEditing(_ textView: UITextView) {
    if textView == txtDescription {
        textView.text.trimNewlinesAndWhitespaces()
        textView.text = textView.text.count > 0 ? textView.text : txtDescripttionPlaceHolder
        resizeTxtDescription()
    }
}

private func resizeTxtDescription() {
    let fixedWidth = self.txtDescription.frame.size.width
    self.txtDescription.sizeThatFits(CGSize(width: fixedWidth, height: CGFloat.greatestFiniteMagnitude))
    let newSize = txtDescription.sizeThatFits(CGSize(width: fixedWidth, height: CGFloat.greatestFiniteMagnitude))

    UIView.animate(withDuration: 0.5) { [weak self] in
        self?.txtDescriptionHeightConstraint.constant = newSize.height
        self?.view.setNeedsLayout()
        self?.view.layoutIfNeeded()
    }
}

设置UITableViewCell之后,声明一个约束在我的情况下保持你的UITextView高度,如 txtDescriptionHeightConstraint ,这样你就可以通过改变约束常量来改变你的UITextView高度。