APP 的编程学习

7.编写Home页面- PageContentView

2017-06-10  本文已影响31人  whong736

1.新建自定义的View :PageContentView  类型为UIView

1.自定义构造函数,传入Frame 和控制器数组,每个页面一个控制器

回到HomeViewController 懒加载 PageContentView

// MARK: 闭包定义,懒加载的PageContentView

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

// 1.确定内容的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

}()

在设置UI的函数添加PageContentView的显示

运行效果:

回到PageContentView开始编写 PageContentView 蓝色区域的具体内容

懒加载闭包创建UICollectionView

// MARK:- 懒加载属性

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

//1.创建layout

let layout = UICollectionViewFlowLayout()

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

layout.minimumLineSpacing = 0

layout.minimumInteritemSpacing = 0

//水平滑动

layout.scrollDirection = .horizontal

//2.创建UICollectionView

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

collectionView.showsHorizontalScrollIndicator = false

//分页设置为true

collectionView.isPagingEnabled = true

//不允许超过内容区域

collectionView.bounces = false

collectionView.scrollsToTop = false

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

return collectionView

}()

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

addSubview(collectionView)

collectionView.frame=bounds

运行效果:

要显示数据,就要实现DataSource的方法

设置collectionView 数据源协议 等于它自己

遵循数据源协议

在设定cell时,需要新建Cell

//定义cell ID

privateletContentCellID ="ContentCellID"

运行效果:可以左右欢动内容区域


上一篇下一篇

猜你喜欢

热点阅读