解決 iOS 11 下 UITableView 的 conten

2018-07-02  本文已影响42人  Kevin追梦先生

使用 iOS 11 測試時,發現 UITableView 的 contentSize 與 contentOffset 無法取得準確的值,導致某一些使用到這些屬性 (例如將表格捲動到指定的高度) 的功能出錯。

這其實是因為 iOS 11 預設會開啟表格的 Self-Sizing。當開啟了這個屬性以後,得到的表格 contentSize 只會是暫時的估算值,等於 estimatedRowHeight 乘上 cell 個數,而不會立即得到正確的 contentSize。當我們有功能去使用到這個值時就會發現和預期的效果不同。

解決方式是將表格的以下三個屬性設成 0,這樣就可以關閉 Self-Sizing 了

_tableView.estimatedRowHeight = 0;

_tableView.estimatedSectionHeaderHeight = 0;

_tableView.estimatedSectionFooterHeight = 0;

Reference:

https://forums.developer.apple.com/thread/81895

http://wetest.qq.com/lab/view/326.html

上一篇 下一篇

猜你喜欢

热点阅读