2016-11-17 8 views

答えて

1

セルの幅と高さがUICollectionView'sの幅と高さで表示され、ラベルとテキストを含むカスタムスペースパディングを実現するには、その内側にUIViewを入れてください。

私はちょうどSampleを作成しました。

カスタムアイデアは、カスタムスペースパディングを実現するためにカスタムコレクションビューのセル内で表示することをすでに提案しています。

ViewControllermyCollectionViewおよびpageControlで2つのIBOutletsを撮影しました。

import UIKit 

class ViewController: UIViewController { 

    @IBOutlet weak var pageControl: UIPageControl! 
    @IBOutlet weak var myCollectionView: UICollectionView! 
    override func viewDidLoad() { 
     super.viewDidLoad() 
     // Do any additional setup after loading the view, typically from a nib. 
    } 
} 

extension ViewController:UICollectionViewDataSource,UICollectionViewDelegateFlowLayout { 

    //MARK:- CollectionView Datasource 
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int { 
     return 3 
    } 

    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell { 
     let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "CustomCollectionViewCell", for: indexPath) as! CustomCollectionViewCell 
     return cell 
    } 

    //MARK:- CollectionView Delegate 
    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize { 
     return CGSize(width: collectionView.frame.size.width, height: collectionView.frame.size.height) 
    } 

    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumLineSpacingForSectionAt section: Int) -> CGFloat { 
     return 0 
    } 

    //MARK:- ScrollView Delegates 
    func scrollViewDidEndDecelerating(_ scrollView: UIScrollView) { 
     let pageWidth = myCollectionView.frame.size.width; 
     let page = floor((myCollectionView.contentOffset.x - pageWidth/2)/pageWidth) + 1 
     pageControl.currentPage = Int(page) 
     print(page) 
    } 
} 

scrollViewDidEndDeceleratingはあなたがどのインデックスに決めるとそれに応じて上記のmapViewを更新することができます。ページ番号0は、その最初のセル(indexPath.row = 0)を示します。 2番目のインデックスに移動すると、2番目のインデックスを意味する1.0が表示されます。

ですUICollectionViewCell

import UIKit 

class CustomCollectionViewCell: UICollectionViewCell { 

    @IBOutlet weak var cellView: UIView! 

    override func awakeFromNib() { 
     super.awakeFromNib() 

     //To make corners round 
     cellView.layer.cornerRadius = 10.0 
    } 
} 

マイビューの階層構造の私のカスタムセルクラス

enter image description here

と出力

enter image description here

はあなたには、いくつかの私を得るホープDEA。

+0

応答ありがとうございました私は必要なものを正確に得ていました –

0

UIScrollViewのようになり、isPagingEnabledtrueに設定します。下の3つのドットはUIPageControlです。

UIScrollViewDelegatescrollViewDidEndDeceleratingを使用して、他のコンテンツの変更を管理してください。

関連する問題