无法在iOS 11 Swift中平滑地展开文本视图

时间:2018-03-02 04:51:03

标签: swift textview

当我点击“阅读更多”按钮时,我尝试展开文本视图。它适用于iOS 11以下的设备,但在iOS 11版本中无法扩展。为什么呢?

当我点击阅读更多按钮时,将运行此代码。

tableView.beginUpdates()
let txtView = cell2.aboutTxtView!
let txtStr = cell2.aboutTxtView.text!
let btn = cell2.aboutReadBtn!

if self.aboutTag == 0{
    let height = getRowHeightFromTextView(strText: txtStr, txtView: txtView)
    cell2.aboutTxtView_HeightConstraint.constant = height
    self.view.layoutIfNeeded()
    btn.setTitle("Show Less", for: .normal)
    self.aboutTag = 1
    self.aboutHeight = 140.0 + height - 84.0
    tableView.endUpdates()
}
else
{
    cell2.aboutTxtView_HeightConstraint.constant = 84
    self.view.layoutIfNeeded()
    btn.setTitle("Read More", for: .normal)
    self.aboutTag = 0
    self.aboutHeight = 140.0
    tableView.endUpdates()
}

func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {

    if indexPath.section == 1
    {
        return aboutHeight
    }
}

iOS 11版本

enter image description here

iOS 11版

enter image description here

我尝试了什么。

(1)

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

(2)

override func viewDidAppear(_ animated: Bool)
{
    super.viewDidAppear(animated)

    if #available(iOS 11, *)
    {
        self.tableView.contentInsetAdjustmentBehavior = .never
    }
}

3 个答案:

答案 0 :(得分:0)

不要更新表视图只更新表视图中的单个单元格并设置单元格的高度。

func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {

    if indexPath.section == 1
    {
        return aboutHeight
    }
    return UITableViewAutomaticDimension
}

然后仅更新关于我单元格。它只会更新单个细胞,因此细胞扩增会很顺利。

// Add your code to update cell height 
self.tableView.beginUpdates()
let indexPath = IndexPath(row: 0, section: 1)
self.tableView.reloadRows(at: [indexPath], with: UITableViewRowAnimation.automatic)
self.tableView.endUpdates()

更新:

self.tableView.beginUpdates()
let indexPath = IndexPath(row: 0, section: 1)

let txtView = cell2.aboutTxtView!
let txtStr = cell2.aboutTxtView.text!
let btn = cell2.aboutReadBtn!

if self.aboutTag == 0{
    let height = getRowHeightFromTextView(strText: txtStr, txtView: txtView)
    cell2.aboutTxtView_HeightConstraint.constant = height
    btn.setTitle("Show Less", for: .normal)
    self.aboutTag = 1
    self.aboutHeight = 140.0 + height - 84.0
}
else
{
    cell2.aboutTxtView_HeightConstraint.constant = 84
    btn.setTitle("Read More", for: .normal)
    self.aboutTag = 0
    self.aboutHeight = 140.0
}
tableView.endUpdates()

func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {

    if indexPath.section == 1
    {
        return aboutHeight
    }
    return UITableViewAutomaticDimension
}

答案 1 :(得分:0)

如果@ Sid的答案无法解决您的问题。

在UI中进行任何更新后,您应该调用layoutIfNeeded,self.view.layoutIfNeeded()。另外,请使用主队列。

DispatchQueue.main.async {
   self.view.layoutIfNeeded()
}

它将更新视图框架。

答案 2 :(得分:0)

我通过添加这三个代码来解决这个问题。

self.tableView.estimatedRowHeight = 0;
self.tableView.estimatedSectionHeaderHeight = 0;
self.tableView.estimatedSectionFooterHeight = 0;

来源:https://stackoverflow.com/a/46350318/8393564
资料来源:https://stackoverflow.com/a/46257601/8393564