簡(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)航欄。