专业java、php、iOS、C++、网页设计、平面设计、网络营销、游戏开发、前端与移动开发培训机构
照片选择器UI搭建
照片选择器
功能相对独立,单独建立
一个项目来开发,在功能开发完成的时候在整合到项目中来.节约开发时间新建项目
- 1.删除
Main.storyboard
- 2.在
TARGETS
里面去掉Main Interface
配置 - 3.新建
PhotoSelector
文件夹 - 4.新建
HMPhotoSelectorViewController
继承UICollectionViewController
5.
AppDelegate
里实现didFinishLaunchingWithOptions
方法func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool { window = UIWindow(frame: UIScreen.mainScreen().bounds) window?.rootViewController = HMPhotoSelectorViewController() window?.makeKeyAndVisible() return true }
- 运行报错:
reason: 'UICollectionView must be initialized with a non-nil layout parameter'
- 在
CZPhotoSelectorViewController
实现默认构造函数// MARK: - 属性 /// collectionView 的流水布局 private var layout = UICollectionViewFlowLayout() /// 构造函数 init() { super.init(collectionViewLayout: layout) }
准备
collectionView
定义prepareCollectionView
方法override func viewDidLoad() { super.viewDidLoad() prepareCollectionView() } private func prepareCollectionView() { // 注册cell collectionView?.register(UICollectionViewCell.self, forCellWithReuseIdentifier: reuseIdentifier) // 设置背景 collectionView?.backgroundColor = UIColor.white // 设置itemSize layout.itemSize = CGSize(width: 80, height: 80) // 设置collectionView的内边距 self.collectionView?.contentInset = UIEdgeInsets(top: 10, left: 10, bottom: 10, right: 10) } override func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int { return maxOfCount } override func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell { let cell = collectionView.dequeueReusableCell(withReuseIdentifier: reuseIdentifier, for: indexPath) cell.backgroundColor = UIColor.brown return cell }
- 1.删除