9

アニメーションの挿入/削除呼び出しを行った後、UITableView のコンテンツ サイズがいつ更新されるのか興味があります。アニメーションが完了していなくても、フレーム サイズ/コンテンツ サイズがすぐに更新されるという点で、ほとんどの [UIView animation...] ブロックのようになると考えましたが、そうではないようです。何か案は?

4

3 に答える 3

2

保留中sizeThatFits()の新しい高さを公開できるようです。contentSize次に、その保留中のサイズを割り当てて、スクロール アニメーションの早期に解決することができます。

このようなもので:

extension UIScrollView {

    var pendingContentSize: CGSize {
        var tallSize = contentSize
        tallSize.height = .greatestFiniteMagnitude
        return sizeThatFits(tallSize)
    }

    func scrollToBottom(animated: Bool) {
        contentSize = pendingContentSize
        let contentRect = CGRect(origin: .zero, size: contentSize)
        let (bottomSlice, _) = contentRect.divided(atDistance: 1, from: .maxYEdge)
        guard !bottomSlice.isEmpty else { return }
        scrollRectToVisible(bottomSlice, animated: animated)
    }

}

次のようなView Controllerコードを書くことができます:

tableView.insertRows(at: [newIndexPath], with: .none)
tableView.scrollToBottom(animated: true)

最後から 2 番目の行まで (古いコンテンツ サイズを使用して) スクロールするのではなく、テーブルを一番下までスクロールします (新しいコンテンツ サイズを使用して)。

于 2017-01-11T01:42:08.197 に答える