天天看點

iOS11_适配總結

1.UITableView的在iOS11上的側滑删除問題

使用一下方法實作側滑删除時,在iOS11以前的系統無問題。在iOS上,出現删除錯亂或卡頓現象。

func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCellEditingStyle, forRowAt indexPath: IndexPath) {
        //
    }
           

解決:

@available(iOS , *)
    func tableView(_ tableView: UITableView, trailingSwipeActionsConfigurationForRowAt indexPath: IndexPath) -> UISwipeActionsConfiguration? {
        tableView.setEditing(false, animated: true)
        let deleteAction = UIContextualAction.init(style: .destructive, title: "删除") { (action, view, completionHandler) in

            //
        }
       let actions = UISwipeActionsConfiguration(actions: [deleteAction])
        return actions
    }
           

2.MJRefresh iOS11适配

使用MJRefresh時,在UITableView中使用beginRefreshing實作下拉重新整理時,頁面無法滾動到頂部。
使用UICollectionView不會出現此現象。
           

解決方法:

if #available(iOS 11.0, *) {
   tableView.estimatedRowHeight = ;
}
           

3.UITableView:預設開啟Self-Sizing

在iOS11下Headers, footers, and cells都預設開啟Self-Sizing。如果目前項目中沒有使用estimateRowHeight屬性,在iOS11的環境下就要注意了,因為開啟Self-Sizing之後,tableView是使用estimateRowHeight屬性的,這樣就會造成contentSize和contentOffset值的變化,如果是有動畫是觀察這兩個屬性的變化進行的,就會造成動畫的異常。
因為在估算行高機制下,contentSize的值是一點點地變化更新的,所有cell顯示完後才是最終的contentSize值。因為不會緩存正确的行高,tableView reloadData的時候,會重新計算contentSize,就有可能會引起contentOffset的變化。iOS11下不想使用Self-Sizing的話,可以通過以下方式關閉:
           
self.tableView.estimatedRowHeight = ;
self.tableView.estimatedSectionHeaderHeight = ;
self.tableView.estimatedSectionFooterHeight = ;