2017-03-18 7 views
1

コレクションビューは、画面の上部に配置されます。コレクションビューは水平で1行あります。コレクションビューの高さでUICollectionViewアイテムのサイズを正方形のサイズに変更するにはどうすればよいですか?

行には、複数のコレクションビューセルを含めることができます。

各セルにはUIImageViewがあります。 UIImageViewにはUIImageが含まれています。

UIImageは写真です。写真のサイズは異なりますが、コレクションビューのセル内では、縦横比1:1で2乗する必要があります。

問題は、正方形の辺がcollectionViewの高さに等しくなるようにコレクションビューのセルを正方形のサイズにすることです。そして、UIImageViewはUIImageを二乗したコレクションビューセル全体を占有する必要があります。

UIImageViewには制約が1:1に設定されています。および収集セルの上部、最上部、および下部。

収集セルは、プログラムで計算し、その大きさがあります。

func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize { 

    let size = collectionView.bounds.size.height 

    return CGSize(width: size, height: size) 
} 

を私は、デバッガで次のエラーを取得する:

[LayoutConstraints] Unable to simultaneously satisfy constraints. 
    Probably at least one of the constraints in the following list is one you don't want. 
    Try this: 
     (1) look at each constraint and try to figure out which you don't expect; 
     (2) find the code that added the unwanted constraint or constraints and fix it. 
    (Note: If you're seeing NSAutoresizingMaskLayoutConstraints that you don't understand, refer to the documentation for the UIView property translatesAutoresizingMaskIntoConstraints) 
(
    "<NSAutoresizingMaskLayoutConstraint:0x6180000968f0 h=--& v=--& UIView:0x7f8c3f61e940.width == 217 (active)>", 
    "<NSAutoresizingMaskLayoutConstraint:0x618000096990 h=--& v=--& UIView:0x7f8c3f61e940.height == 100 (active)>", 
    "<NSLayoutConstraint:0x618000096030 UIImageView:0x7f8c3f61eae0.width == UIImageView:0x7f8c3f61eae0.height (active)>", 
    "<NSLayoutConstraint:0x618000096120 H:[UIImageView:0x7f8c3f61eae0]-(0)-| (active, names: '|':UIView:0x7f8c3f61e940)>", 
    "<NSLayoutConstraint:0x618000096170 V:[UIImageView:0x7f8c3f61eae0]-(0)-| (active, names: '|':UIView:0x7f8c3f61e940)>", 
    "<NSLayoutConstraint:0x6180000961c0 H:|-(0)-[UIImageView:0x7f8c3f61eae0] (active, names: '|':UIView:0x7f8c3f61e940)>", 
    "<NSLayoutConstraint:0x618000096210 V:|-(0)-[UIImageView:0x7f8c3f61eae0] (active, names: '|':UIView:0x7f8c3f61e940)>" 
) 

Will attempt to recover by breaking constraint 
<NSLayoutConstraint:0x618000096030 UIImageView:0x7f8c3f61eae0.width == UIImageView:0x7f8c3f61eae0.height (active)> 

Make a symbolic breakpoint at UIViewAlertForUnsatisfiableConstraints to catch this in the debugger. 
The methods in the UIConstraintBasedLayoutDebugging category on UIView listed in <UIKit/UIView.h> may also be helpful. 

それを動作させるには?

+0

ので、UIは、あなたが望んだとして表示されます。あなたの唯一の問題は制約の競合ですか? – hasan83

+0

セルとimageViewの間にパディングがありますか? –

+0

@ hasan83 が壊れており、画像が二乗されていないように見えます。 –

答えて

1

縦横比制約と右制約の両方を必要としません。 1つで十分です。しかし、論理的には、両方の場合は、あなたのサイズの計算が正しい場合は競合してはいけません。

あなたの計算はよさそうです。したがって、あなたの計算に影響を与える可能性が心の中のものは、(ヘッダサイズ、フッタのサイズ、インセット間隔)である

これに影響を与える可能性があるもう一つは、uiviews完全なレイアウト機能前のviewDidLoadのコレクションビューデータ をリロードされます。

enter image description here

+0

セルサイズを0に設定した以外はすべてがあります。 –

+0

ビューに最終的なレイアウトが行われるまでに何回か競合が発生します。すべての値が期待どおりであるかどうかを確認するためにデバッグを行うことができます。あなたが必要としないアスペクト比の制約を取り除きます。セルのサイズを設定しており、上部、ボット、左、右の制約でイメージビューのサイズを変更します。 – hasan83

+0

アスペクト比を削除しましたが、エラーは次のように変更されました:** UICollectionViewFlowLayoutの動作は定義されていません。 項目の高さはUICollectionViewの高さからセクションの上下の値を引いた値マイナスコンテンツは上下の値を挿入します。** –

関連する問題