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

问题描述 投票:0回答:3

我点击“阅读更多”按钮时尝试展开文本视图。它适用于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
    }
}
swift textview
3个回答
0
投票

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

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

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

它将更新视图框架。


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
}

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

© www.soinside.com 2019 - 2024. All rights reserved.