UIScrollView、UICollectionView、UITableView頂部空白解決辦法

簡(jiǎn)介

UIScrollView、UICollectionView、UITableView這些視圖,是最常用的視圖類;但是我對(duì)它們是又愛(ài)又恨,在應(yīng)用的過(guò)程中,會(huì)出現(xiàn)各種意想不到的驚喜。這篇文章用于記錄,上述三個(gè)類的頂部空白的解決方法。

1.UIScrollView頂部空白的相關(guān)屬性

1.1 automaticallyAdjustsScrollViewInsets屬性:

  是iOS7以后UIViewController的新增屬性;
  在iOS11以后廢除;
  當(dāng)設(shè)置為YES時(shí)(默認(rèn)YES),如果視圖里面存在唯一一個(gè)UIScrollView或其子類View,那么它會(huì)自動(dòng)設(shè)置相應(yīng)的內(nèi)邊距,這樣可以讓scroll占據(jù)整個(gè)視圖,又不會(huì)讓導(dǎo)航欄遮蓋。

1.2 contentInsetAdjustmentBehavior屬性:

  是iOS11新增的UIScrollView的屬性;
  是對(duì)廢除automaticallyAdjustsScrollViewInsets的增進(jìn)補(bǔ)充;
  當(dāng)該屬性的制為.never時(shí),與automaticallyAdjustsScrollViewInsets為NO時(shí),效果相同,即不考慮頂部導(dǎo)航欄和安全區(qū)域等因素,填充滿整個(gè)視圖
  UICollectionView、UITableView是UIScrollView的子視圖,設(shè)置該屬性與UIScrollView效果等同。

1.3 代碼設(shè)置上述屬性

       if #available(iOS 11, *) {
           //系統(tǒng)版本高于11
           tableView.contentInsetAdjustmentBehavior = .never
       }
       else {
           self.automaticallyAdjustsScrollViewInsets = false
       }

2.UITableView頂部空白的相關(guān)設(shè)置和方法

2.1影響UITableView頂部空白的因素

  1.contentInsetAdjustmentBehavior或者automaticallyAdjustsScrollViewInsets屬性的設(shè)置,同UIScrollView;
  2.tableHeaderView屬性的設(shè)置;
  3.UITableView分區(qū)頭部視圖sectionHeader的設(shè)置;

2.2代碼設(shè)置上述屬性和方法

        tableView.tableHeaderView = nil // 在iOS12以前,該操作會(huì)導(dǎo)致tableView頂部空白
        tableView.tableHeaderView = UIView.init()   // 在iOS12以前,該操作會(huì)導(dǎo)致tableView頂部空白
        tableView.tableHeaderView = UIView.init(frame: CGRect.init(x: 0, y: 0, width: kScreenWidth, height: CGFloat.leastNonzeroMagnitude)) // 該操不會(huì)導(dǎo)致tableView頂部空白

同理,tableFooterView也是如此。footer和header只要設(shè)置了任意一個(gè)都會(huì)使兩個(gè)地方都出現(xiàn)空白。

    func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
        if section == 0 {
            return 30  // 設(shè)置分區(qū)頂部高度,cell分區(qū)頂部的空白
        }
        else {
            return CGFloat.leastNonzeroMagnitude  //  設(shè)置分區(qū)頂部高度,cell分區(qū)頂部無(wú)空白
        }
    }
    
    func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
        if section == 2 {  // 設(shè)置頂部分區(qū)視圖
            let headerView = UIView.init()
            headerView.frame = CGRect.init(x: 0, y: 0, width: kScreenWidth, height: 30)
            return headerView
        }
        else {
            return nil  // 沒(méi)有頂部分區(qū)設(shè)置為nil
        }
    }

同理,sectionFooter也是如此。

3.UICollectionView頂部空白的相關(guān)設(shè)置和方法

3.1影響UICollectionView頂部空白的因素

  1.contentInsetAdjustmentBehavior或者automaticallyAdjustsScrollViewInsets屬性的設(shè)置,同UIScrollView;
  2.UICollectionView分區(qū)頭部視圖sectionHeader的設(shè)置;

3.2代碼設(shè)置上述屬性和方法

    func collectionView(_ collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, at indexPath: IndexPath) -> UICollectionReusableView {  // 初始化sectionHeader
            let headerView = AKPracticeHeaderCollectionReusableView.section(collectionView: collectionView, indexPath: indexPath)
            headerView.timeTitle = self.workList?[indexPath.section].title
            return headerView
    }

    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, referenceSizeForHeaderInSection section: Int) -> CGSize {
        return .zero  // 設(shè)置sectionHeader的尺寸
    }

4.UIScrollView、UICollectionView、UITableView影響頂部空白的其它因素

4.1UINavigationBar的透明度設(shè)置

當(dāng)使用系統(tǒng)的UINavigationBar時(shí),self.navigationController.navigationBar.translucent當(dāng)這個(gè)屬性設(shè)為false時(shí),tableview會(huì)在上方留出64.f的高度給導(dǎo)航欄。

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

友情鏈接更多精彩內(nèi)容