CollectionViewCell
のマージンは無しで考えていますが、以下のようにすることでセルを中心に配置できました、お試しください。
swift
1import UIKit
2
3class ViewController: UIViewController, UICollectionViewDataSource, UICollectionViewDelegate {
4
5 @IBOutlet weak var collectionView: UICollectionView!
6
7 var cellWidth: CGFloat = UIScreen.mainScreen().bounds.width / 3
8
9 override func viewDidLoad() {
10 super.viewDidLoad()
11
12 }
13
14 // MARK: - UICollectionViewDelegate
15 func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
16 let cell = collectionView.dequeueReusableCellWithReuseIdentifier("Cell", forIndexPath: indexPath) as! CustomCell
17 cell.imageView.image = UIImage(named: "swift")
18 cell.backgroundColor = UIColor.yellowColor()
19 return cell
20 }
21
22 func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
23 return 13
24 }
25
26 // Cell Size Change
27 func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAtIndexPath indexPath: NSIndexPath) -> CGSize {
28 return CGSizeMake(cellWidth, 150)
29 }
30
31 func scrollViewWillEndDragging(scrollView: UIScrollView, withVelocity velocity: CGPoint, targetContentOffset: UnsafeMutablePointer<CGPoint>) {
32
33 targetContentOffset.memory.x = round(targetContentOffset.memory.x / cellWidth) * cellWidth
34 }
35
36 override func didReceiveMemoryWarning() {
37 super.didReceiveMemoryWarning()
38 }
39}
40
41// Custom Cell Class
42import UIKit
43
44class CustomCell: UICollectionViewCell {
45 @IBOutlet weak var imageView: UIImageView!
46}
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2016/09/16 22:19