1.collectionView
设置UICollectionViewLayout:
flowLayout
1.设置Cell的大小 //itemSize,默认是50,50
2.滚动方向 //scrollDirection
cell的最小间距(不设置就是默认)
cell的行间距(不设置就是默认)
let layout = UICollectionViewFlowLayout()
let width = self.view.bounds.width
// layout.itemSize = CGSize(width: 100, height: 100)//默认大小50,50
// layout.scrollDirection = .Horizontal //默认是垂直方向
// layout.minimumInteritemSpacing = 10 //排列时cell间的最小间距,默认10
// layout.minimumLineSpacing = 0 //行间距,默认10
// layout.sectionInset = UIEdgeInsetsMake(10, 10, 0, 10) //cell距离collection编剧的大小
collectionView = UICollectionView(frame:(self.view.bounds), collectionViewLayout: layout)
self.view.addSubview(collectionView)
collectionView.dataSource = self
collectionView.delegate = self
collectionView.backgroundColor = UIColor.whiteColor()
collectionView.pagingEnabled = true
//注册cell
collectionView.registerClass(UICollectionViewCell.self, forCellWithReuseIdentifier: "cell")
//注册头部
collectionView.registerClass(HeaderCollectionReusableView.self, forSupplementaryViewOfKind: UICollectionElementKindSectionHeader, withReuseIdentifier: "header")
datasource:
需要实现的方法
1.cell的个数//numberOfItemsInSection
2.创建cell//cellForItemAtIndexPath
collectionView自动滚动:
通过计时器实现找到要滚动到的cell
实现方法:
1.获取偏移量
2.根据偏移量获取cell的位置得出当前ITEM的IndexPath
3.根据IndexPath滚动cell collectionView.scrollToItemAtIndexPath
实现循环:
4.到最后一个item的时候,无动画跳到第一个,并且有动画直接执行滚动到第二个(第一个和最后一个一样的视图)
let offset = collectionView.contentOffset//获取偏移量
let indexPath = collectionView.indexPathForItemAtPoint(CGPoint(x: offset.x, y:10))
print(indexPath?.item)
//通过偏移量找到需要滚动到的cell(这个CGPoint需要偏移后在cell所在的区域内)
if indexPath?.item == 4 {
collectionView.scrollToItemAtIndexPath(NSIndexPath(forItem: 0, inSection: 0), atScrollPosition: .Left, animated: false)
collectionView.scrollToItemAtIndexPath(NSIndexPath(forItem: 1, inSection: 0), atScrollPosition: .Left, animated: true)
}
else {
collectionView.scrollToItemAtIndexPath(NSIndexPath(forItem: indexPath!.item + 1, inSection: 0), atScrollPosition: .Left, animated: true)
}
2.导航控制器
titleview
导航条中间的一个view(通过设置)
通过AppDelegate实现代码启动:
self.window = UIWindow(frame: UIScreen.mainScreen().bounds)
self.window?.makeKeyAndVisible()//让window成为keyWindow并且可视化
let firstCtr = ViewController()
let nagvigation = UINavigationController(rootViewController: firstCtr)
self.window?.rootViewController = nagvigation//导航控制器成为根视图
nagvigation.hidesBarsOnSwipe = true //导航控制器附近上下滑动隐藏
// nagvigation.hidesBarsOnTap = true //点击隐藏或者显示
// nagvigation.navigationBar.barStyle = UIBarStyle.BlackOpaque//导航条的类型
// nagvigation.navigationBar.backgroundColor = UIColor.blueColor() //背景色,只在导航栏下面很浅的一层
nagvigation.navigationBar.barTintColor = UIColor.blueColor()//导航栏的颜色
nagvigation.navigationBar.tintColor = UIColor.redColor() //文字/图片,在导航控制器上面的内容颜色(默认是蓝色)
导航控制器隐藏的几种属性:
// hidesBarsOnSwipe = true //导航控制器附近上下滑动隐藏
// hidesBarsOnTap = true//点击隐藏或者显示
// hidesBarsWhenVerticallyCompact//屏幕横方向的时候隐藏
// hidesBarsWhenKeyboardApperas//当键盘显示的时候
// nagvigation.navigationBar.barStyle = UIBarStyle.BlackOpaque//导航条的类型
tips:
- 图片总是显示原始颜色,选择图片渲染模式(总是保持原始的)
let image = originImage?.imageWithRenderingMode(.AlwaysOriginal)
2.导航栏的属性只能在delegate上设置
每一个页面上都能设置当前页面的navigationItem
3.定制的cell或者head,foot 继承,重写(和tableview类似)
网友评论