關(guān)鍵詞: 開發(fā), iOS, tableView,
使用SnapKit+FDTemplateLayoutCell時(shí)cell無法計(jì)算高度
FDTemplateLayoutCell是一款很好用的自動(dòng)計(jì)算cell高度的開源框架,然鵝現(xiàn)在越來越多的同學(xué)會(huì)使用約束代碼構(gòu)建布局.比如swift的SnapKit或者OC的Masonry
但有的時(shí)候會(huì)遇到一些詭異的問題, 例如在確定cell內(nèi)容在約束代碼沒有錯(cuò)誤的情況下卻無法計(jì)算cell高度,
例如下面這樣

查了一些資料 確實(shí)有少部分同學(xué)遇到了同樣的問題,而且經(jīng)過測(cè)試bug 不是絕對(duì)成立
最后在翻看github時(shí)看到一位同學(xué)這樣描述到

察覺到這可能是自己項(xiàng)目中的問題本質(zhì),
因?yàn)樵陧?xiàng)目中 用到了snapKit, tableView 肯定也是通過約束布局編寫的
tableView.snp.makeConstraints { (mark) in
mark.top.left.right.equalTo(0)
mark.height.equalTo(KMainScreenSize.height - 64 - KAdaptedSize(50))
}
如上代碼 沒有指定寬度 只是進(jìn)行了相對(duì)布局
如果 你開啟了fd的日志打印
tableView.fd_debugLogEnabled = true
會(huì)看到這么一行提示
2017-08-17 16:51:04.771 sjt2[69976:26043709] ** FDTemplateLayoutCell ** layout cell created - PostSerachTableViewCell
2017-08-17 16:51:04.775 sjt2[69976:26043709] [FDTemplateLayoutCell] Warning once only: Cannot get a proper cell height (now 0) from '- systemFittingSize:'(AutoLayout). You should check how constraints are built in cell, making it into 'self-sizing' cell.
2017-08-17 16:51:04.776 sjt2[69976:26043709] ** FDTemplateLayoutCell ** calculate using sizeThatFits - 45
結(jié)合上面同學(xué)提到的問題,給tableView約束添加完成之后強(qiáng)制刷新view,讓后面調(diào)用systemFittingSize時(shí)可以獲取到值
tableView.snp.makeConstraints { (mark) in
mark.top.left.right.equalTo(0)
mark.height.equalTo(KMainScreenSize.height - 64 - KAdaptedSize(50))
}
self.view.layoutIfNeeded()
或者也可以直接不用約束寫frame
lazy var tableView:UITableView = {
let t = UITableView(frame: CGRect(x: 0, y: 0, width: KMainScreenSize.width, height: KMainScreenSize.height - KAdaptedSize(50) - 64))
t.separatorStyle = .none
t.delegate = self
t.dataSource = self
return t
}()
重新運(yùn)行代碼問題解決,具體問題原因參考
layoutSubviews()
TableView,iOS11 frame 全屏模式下 導(dǎo)航欄占位
關(guān)鍵詞: iOS11,tableview, collectionView, 導(dǎo)航欄, 空白
在iOS11 上 出現(xiàn)了 一個(gè)適配問題, 當(dāng)隱藏導(dǎo)航欄且 Y 坐標(biāo)=0時(shí), 狀態(tài)欄依然出現(xiàn)占位空白
主要原因是 iOS 11 上 automaticallyAdjustsScrollViewInsets 被廢棄;
同時(shí)實(shí)用 tableView,collectionView,
之前可以直接寫在基類里, 現(xiàn)在需要在子類中調(diào)用了
使用下面方法替換
func resetAutomaticallyAdjustsScrollViewInsetsWithIOS11(subView:UIScrollView?) {
if #available(iOS 11.0, *) {
if let view = subView {
view.contentInsetAdjustmentBehavior = .never
}
} else {
self.automaticallyAdjustsScrollViewInsets = false
}
}
TableView,iOS11 刷新某一行時(shí) 導(dǎo)致 UI 界面跳動(dòng)
關(guān)鍵詞: 刷新,跳動(dòng)
問題是 iOS 11 的新特性導(dǎo)致的
需要在創(chuàng)建 tableView 處加入
self.tableView.estimatedRowHeight = 0;