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ū)域
