菜鸟教程小白 发表于 2022-12-11 19:18:01

ios - UIScrollView 有时会在更改内部 UITableView 的高度时失去滚动


                                            <p><p>好的,我有简单的 <code>UIScrollView</code> 和两个 subview :<code>UIView</code> 和 <code>UITableView</code> 设置为垂直模式。它们都有自己的高度限制:</p>

<p>我是这样设置的:</p>

<pre><code>tableViewHeightConstraint.constant = CGFloat(height) //height is calculated based on number of cells multiplied 130

calendarViewHeightConstraint.constant = UIApplication.shared.keyWindow!.rootViewController!.traitCollection.isIpad ? 630 : 470
</code></pre>

<p>但有时当我滚动到底部,然后在表格 View 中添加一个新单元格并重新计算高度时, ScrollView 的滚动似乎被锁定,我可以弹跳但滚动消失,我只能弹跳,虽然我知道我可以滚动到屏幕上当前 View 上方或下方的内容。可能是什么原因?</p>

<p>我真的必须这样做。不可能将我的 View 放在表格 View 标题 View 中。请让我们离开这个话题;)</p>

<p>这是我重新计算高度的地方:</p>

<pre><code>func controllerDidChangeContent(_ controller: NSFetchedResultsController&lt;NSFetchRequestResult&gt;) {

      tableView.endUpdates()
      updateView()
      updateTableViewHeight()
}

private func updateTableViewHeight() {

    var height = goalFetchedResultsController.fetchedObjects!.count * 130

    height += 45

    if MyType.selected.hasMonthReport {
      height += 130
    }

    if MyType.selected.hasAnnualReport {
      height += 70
      height += 130
    }

    tableViewHeightConstraint.constant = CGFloat(height)
}
</code></pre>

<p>新单元格被添加到 <code>NSFetchedResultsController</code> 的委托(delegate)中。</p></p>
                                    <br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
                                            <p><p>我认为您需要更改 TableView 的 <strong>height</strong> 的计算方式,例如,</p>

<pre><code>var height = tablView.contentSize.heigh // You can do + or - here

// your other stuff of calculation.
tableViewHeightConstraint.constant = CGFloat(height)

self.view.layoutIfNeeded() // You need to call after change constraint.
</code></pre></p>
                                   
                                                <p style="font-size: 20px;">关于ios - UIScrollView 有时会在更改内部 UITableView 的高度时失去滚动,我们在Stack Overflow上找到一个类似的问题:
                                                        <a href="https://stackoverflow.com/questions/47301428/" rel="noreferrer noopener nofollow" style="color: red;">
                                                                https://stackoverflow.com/questions/47301428/
                                                        </a>
                                                </p>
                                       
页: [1]
查看完整版本: ios - UIScrollView 有时会在更改内部 UITableView 的高度时失去滚动