2016-07-20 4 views
4

insetForSectionAtIndexメソッドを使用して、コレクションビューのセクションのcontentInsetを設定しましたが、そのインセットをセクションのヘッダーに適用しません。ヘッダーの幅は画面の幅と同じにする必要があります。異なるコンテンツを適用する方法コンテンツセルに対してのみUICollectionViewのヘッダーではなくコンテンツセルにのみ適用します

ConentInset

func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, insetForSectionAtIndex section: Int) -> UIEdgeInsets { 
    if section == 1 { 
     return UIEdgeInsets(top: 0, left: 15, bottom: 0, right: 15) 
    } 

    return UIEdgeInsets(top: 0, left: 0, bottom: 0, right: 0) 
} 

ヘッダー

override func collectionView(collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, atIndexPath indexPath: NSIndexPath) -> UICollectionReusableView { 

    let header = collectionView.dequeueReusableSupplementaryViewOfKind(kind, withReuseIdentifier: opsMainDescriptionSegmentedControlCellId, forIndexPath: indexPath) as! MyHeader 

    return header 
} 

答えて

0

あなたはこれを行うことはできません。 UICollectionViewsヘッダーは、UICollectionViewと同じ幅である必要があります。ヘッダーの長さをUICollectionViewより短くしたい場合は、別のUIViewヘッダーを使用し、ヘッダーをクリアに設定することをお勧めします。そうすれば、その幅はUICollectionViewの幅よりも短くなるように見えます。

+0

ヘッダーの幅をUICollectionView以上で、画面の幅に合わせることは可能でしょうか? – Bigair

+0

あなたの質問が間違っているようです。あなたが現在やっていることは、うまくいくはずです。 UICollectionViewの幅が親ビューの幅と同じに設定されていることを確認してください。 – Tander

+0

ヘッダーの幅がcollectionViewより広い場合、XCodeはエラーメッセージを記録します。アプリケーションはクラッシュしませんが、ヘッダーの幅はコレクションビューの幅にリサイズされます。 – Bigair

3

私はちょうど私のプロジェクトの一つでこの全く同じことをしなければなりませんでした。私が適用さ

ソリューション:

をストーリーボードでは、私のコレクションビューは、自動レイアウトの制約でフルスクリーンを占めます。 viewDidLoadで

collectionView.contentInset = UIEdgeInsets(top: 0, left: 0, bottom: 0, right: 0) 

私は自分にcollectionViewデリゲートを設定しても

UICollectionViewDelegateFlowLayoutプロトコルに準拠しています。私は別のUIEdgeInsets

func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, insetForSectionAt section: Int) -> UIEdgeInsets { 

     return UIEdgeInsets(top: 15, left: 20, bottom: 15, right: 20) 

    } 

を返し、それは私のために働いた

あなたは、あなたが使用したメソッドへのアクセス権を持っています。

最後にどのように見えたかを見てください。

enter image description here

希望は、これは他人を助けることができるようになります。

関連する問題