コレクションビューは、画面の上部に配置されます。コレクションビューは水平で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.
それを動作させるには?
ので、UIは、あなたが望んだとして表示されます。あなたの唯一の問題は制約の競合ですか? – hasan83
セルとimageViewの間にパディングがありますか? –
@ hasan83が壊れており、画像が二乗されていないように見えます。 –