大家在做iOS11 適配scrollView滾動(dòng)導(dǎo)致的頁面位置不對(duì),是否用過了如下代碼
?// iOS11 UIScrollView適配
? ? if(@available(iOS11.0, *)) {
? ? ? ? UIScrollView.appearance.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
? ? ? ? UITableView.appearance.estimatedRowHeight = 0;
? ? ? ? UITableView.appearance.estimatedSectionFooterHeight = 0;
? ? ? ? UITableView.appearance.estimatedSectionHeaderHeight = 0;
? ? ? ? UITableView.appearance.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
? ? }
重點(diǎn)討論的是如下代碼:TāC鹊帧煌抒!這一行慎重填寫M尽O街凇琢唾!
? ? ? ? UITableView.appearance.estimatedRowHeight = 0;
首先在Xcode的項(xiàng)目中都是默認(rèn)開啟自動(dòng)布局载荔,也就是說你可以通過約束來決定cell的高度,不需要通過代理協(xié)議和設(shè)置UITableView.rowHeight來實(shí)現(xiàn)cell高度采桃,只需要你將cell的高度約束設(shè)置好懒熙,系統(tǒng)會(huì)自動(dòng)計(jì)算。
//系統(tǒng)默認(rèn)的是
UITableView.appearance.estimatedRowHeight =?UITableViewAutomaticDimension;
但是如果你實(shí)現(xiàn)了(假如你的tableview有大量的數(shù)據(jù)和計(jì)算高度普办,不建議實(shí)現(xiàn))
? ? ? ? UITableView.appearance.estimatedRowHeight = 0;
優(yōu)點(diǎn):你可以從容的通過tableview.contentSize來獲取tableView的正確內(nèi)容高度工扎。
缺點(diǎn):會(huì)導(dǎo)致你的所有自動(dòng)計(jì)算高度失效,每次tableview reloadData 都會(huì)重新計(jì)算tableview的所有內(nèi)容高度衔蹲,也就是所有返回高度的代理都會(huì)執(zhí)行肢娘,從而導(dǎo)致刷新的時(shí)候占用高額的資源,特別是有大量數(shù)據(jù)的時(shí)候存在卡頓的現(xiàn)象舆驶。
中間解決方案:每個(gè)tableView根據(jù)具體的業(yè)務(wù)需求來使用tableView.estimatedRowHeight =?UITableViewAutomaticDimension;
而不是UITableView.appearance.estimatedRowHeight=0.