2017-09-21 20 views
0

私は水平スクロールでUICollectionViewを持っています。ここに私のcollectionViewです:UICollectionView水平スクロール

fileprivate(set) lazy var collectionView: UICollectionView = { 
     let width = UIScreen.main.bounds.width.multiplied(by: 0.9) 
     let layout: UICollectionViewFlowLayout = UICollectionViewFlowLayout() 
     layout.itemSize = CGSize(width: width, height: 50) 

     layout.sectionInset = UIEdgeInsets(top: 20, left: 20, bottom: 10, right: 20) 
     layout.scrollDirection = .horizontal 
     layout.minimumLineSpacing = 20 

     let collectionView = UICollectionView(frame: CGRect(x: 0, y: 0, width: self.frame.width, height: 50), collectionViewLayout: layout) 
     collectionView.translatesAutoresizingMaskIntoConstraints = false 
     collectionView.backgroundColor = .red 
     collectionView.isPagingEnabled = true 
     return collectionView 
    }() 

、それはそのようになっています

enter image description here

あなたは私がページング効果をしたいので、私はコード内のcollectionView.isPagingEnabled = trueを持って見るように。それでは、私が達成しようとしている他のすべてのページ内の項目は、(左と右の20の間隔)上の写真のように見えるようにすることですが、これまでのところ、私は取得しています:

enter image description here

を任意のアイデア/どのように希望の動作に到達するヒント?

+0

私はストーリーボードを使用していない、あなたは再びコードを見れば、私はすでにプロパティを設定しました。 – radioaktiv

+0

これを試してください。 isScrollEnabled:true –

+0

layout.itemSize = CGSize(幅:幅 - 40、高さ:50) –

答えて

1

ここはUICollectionViewDelegateFlowLayout私がテストプロジェクトであなたの望みを達成するために使ったものです。あなたのコードで

func collectionView(_ collectionView: UICollectionView, 
        layout collectionViewLayout: UICollectionViewLayout, 
        sizeForItemAt indexPath: IndexPath) -> CGSize { 
    return CGSize(width: UIScreen.main.bounds.width.multiplied(by: 0.9), height: 50.0) 
} 

// item spacing = vertical spacing in horizontal flow 
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumInteritemSpacingForSectionAt section: Int) -> CGFloat { 
    return (UIScreen.main.bounds.width.multiplied(by: 0.1)) 
} 

// line spacing = horizontal spacing in horizontal flow 
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumLineSpacingForSectionAt section: Int) -> CGFloat { 
    return (UIScreen.main.bounds.width.multiplied(by: 0.1)) 
} 

func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, insetForSectionAt section: Int) -> UIEdgeInsets { 
    return UIEdgeInsets(top: 0, left: (UIScreen.main.bounds.width.multiplied(by: 0.1)/2.0), bottom: 0, right: (UIScreen.main.bounds.width.multiplied(by: 0.1)/2.0)) 
} 

それはそのようになるだろう:

fileprivate(set) lazy var collectionView: UICollectionView = { 
    let width = UIScreen.main.bounds.width.multiplied(by: 0.9) 
    let layout: UICollectionViewFlowLayout = UICollectionViewFlowLayout() 
    layout.itemSize = CGSize(width: width, height: 50) 

    layout.sectionInset = UIEdgeInsets(top: 20, left: UIScreen.main.bounds.width.multiplied(by: 0.1)/2.0, bottom: 10, right: UIScreen.main.bounds.width.multiplied(by: 0.1)/2.0) 
    layout.scrollDirection = .horizontal 
    layout.minimumLineSpacing = UIScreen.main.bounds.width.multiplied(by: 0.1) 
    layout.minimumInteritemSpacing = UIScreen.main.bounds.width.multiplied(by: 0.1) // or any value you want 

    let collectionView = UICollectionView(frame: CGRect(x: 0, y: 0, width: self.frame.width, height: 50), collectionViewLayout: layout) 
    collectionView.translatesAutoresizingMaskIntoConstraints = false 
    collectionView.backgroundColor = .red 
    collectionView.isPagingEnabled = true 
    return collectionView 
}() 
+1

パーフェクトsoulution!ありがとう:) – radioaktiv

関連する問題