Objective
API not familiar with the cause of the problem, take it for granted to understand that there will be problems, here to record the use of Uicollectionview.
Body
Trap One: minimumlinespacing, minimuminteritemspacing
It's easy to set these two properties to 0, which are the minimum line spacing and the minimum column spacing, and note that the minimum!! That means you can actually > 0, not spacing is 0.
Trap Two: Sectioninset
Sets the margins of the cell. At first I thought that is each cell's margin, the adjacent will overlay the effect, actually is not such, this attribute only guarantees the spacing between adjacent, does not stack!!
Understanding the above two traps, we can accurately calculate the size of the Cell, and then set the size of the itemsize will be correct. For example:
Let item_min_width:cgfloat =
item_spacing:cgfloat = 6
func resizecollectionview (size:cgsize) {
if L ET layout = CollectionView? Collectionviewlayout as? uicollectionviewflowlayout {
layout.minimumlinespacing = item_spacing
layout.minimuminteritemspacing = ITEM _spacing
var itemCount = Int (size.width/item_min_width)
if ItemCount = = 0 {
ItemCount = 1
}
if ITEM Count = = 1 {
layout.itemsize = Cgsizemake (size.width, Size.width * 10/16)
Layout.sectioninset = Uiedgeinsetsma Ke (6, 0, 0, 0)
} else {let
width = (size.width-cgfloat (itemCount + 1)) * item_spacing)/cgfloat (ItemCount) C15/>layout.itemsize = Cgsizemake (width, Width * 10/16)
Layout.sectioninset = Uiedgeinsetsmake (item_spacing, ITEM _spacing, 0, item_spacing)
}
CollectionView? Layoutifneeded ()
}
}
Code Description:
The size of the current view, the dynamic calculation of cell sizes, you can easily fit Iphone/ipad, a single column hides both sides of the margin, multiple columns show both sides of the gap.
The above is a friend of iOS Uicollectionview usage and description, hoping to help iOS development.