7.編寫Home頁面- PageContentView

1.新建自定義的View :PageContentView ?類型為UIView

1.自定義構(gòu)造函數(shù),傳入Frame 和控制器數(shù)組,每個頁面一個控制器

回到HomeViewController 懶加載 PageContentView


// MARK: 閉包定義,懶加載的PageContentView

fileprivate lazy var pageContentView: PageContentView = { [weak self] in

// 1.確定內(nèi)容的Frame

let contentH = kScreenH - kStatusBarH - kNavigationBarH - kTitleViewH - kTabbarH

//定義contentFrame

let contentFrame = CGRect(x: 0, y: kStatusBarH + kNavigationBarH + kTitleViewH, width: kScreenW, height: contentH)

var childVcs = [UIViewController]()

for _ in 0..<4{

let vc = UIViewController()

vc.view.backgroundColor = UIColor.randomColor()

childVcs.append(vc)

}

let contentView = PageContentView(frame: contentFrame, childVcs: childVcs, parentViewController: self!)

return contentView

}()

在設(shè)置UI的函數(shù)添加PageContentView的顯示

運(yùn)行效果:


回到PageContentView開始編寫 PageContentView 藍(lán)色區(qū)域的具體內(nèi)容

懶加載閉包創(chuàng)建UICollectionView

// MARK:- 懶加載屬性

fileprivate lazy var collectionView : UICollectionView = { [weak self] in

//1.創(chuàng)建layout

let layout = UICollectionViewFlowLayout()

layout.itemSize = (self?.bounds.size)!

layout.minimumLineSpacing = 0

layout.minimumInteritemSpacing = 0

//水平滑動

layout.scrollDirection = .horizontal

//2.創(chuàng)建UICollectionView

let collectionView = UICollectionView(frame: CGRect.zero, collectionViewLayout: layout)

collectionView.showsHorizontalScrollIndicator = false

//分頁設(shè)置為true

collectionView.isPagingEnabled = true

//不允許超過內(nèi)容區(qū)域

collectionView.bounces = false

collectionView.scrollsToTop = false

//collectionView.register(UICollectionViewCell.self, forCellWithReuseIdentifier: ContentCellID)

return collectionView

}()

//2.添加UICollectionView,用于Cell中存放控制器的View

addSubview(collectionView)

collectionView.frame=bounds

運(yùn)行效果:

要顯示數(shù)據(jù),就要實(shí)現(xiàn)DataSource的方法

設(shè)置collectionView 數(shù)據(jù)源協(xié)議 等于它自己

遵循數(shù)據(jù)源協(xié)議

在設(shè)定cell時,需要新建Cell

//定義cell ID

privateletContentCellID ="ContentCellID"

運(yùn)行效果:可以左右歡動內(nèi)容區(qū)域



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

相關(guān)閱讀更多精彩內(nèi)容

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