一店量、 Using the Flow Layout
You can arrange items in your collection views using a concrete layout object, the UICollectionViewFlowLayout class. The flow layout implements a line-based breaking layout, which means that the layout object places cells on a linear path and fits as many cells along that line as it can. When the layout object runs out of room on the current line, it creates a new line and continues the layout process there. Figure 3-1 shows what this looks like for a flow layout that scrolls vertically. In this case, lines are laid out horizontally with each new line positioned below the previous line. The cells in a single section can be optionally surrounded with section header and section footer views.
Figure 3-1 Laying out sections and cells using the flow layout
你可以使用具體的布局對象(UICollectionViewFlowLayout類)在你的集合視圖中排列items置森。流布局實現(xiàn)了基于行的中斷布局持灰,這意味著布局對象將cells放置在線性路徑上,并盡可能多地沿著該線填充cells拧晕。當布局對象在當前行的空間不足時十酣,將創(chuàng)建一個新行并繼續(xù)布局過程。圖3-1顯示了垂直滾動的流布局的樣子蚊丐。在這種情況下,線被水平放置艳吠,每條新線位于前一行的下方吠撮。單個section中的cells可以選擇性地被section header and section footer視圖包圍。
Figure 3-1 Laying out sections and cells using the flow layout
You can use the flow layout to implement grids, but you can also use it for much more. The idea of a linear layout can be applied to many different designs. For example, rather than having a grid of items, you can adjust the spacing to create a single line of items along the scrolling dimension. Items can also be different sizes, which yields something more asymmetrical than a traditional grid but that still has a linear flow to it. There are many possibilities.
你可以使用流布局來實現(xiàn)網(wǎng)格讲竿,但你也可以使用它實現(xiàn)更多效果。線性布局的想法可以應用于許多不同的設計弄屡。例如题禀,除了可以使用網(wǎng)格布局items,你可以調(diào)整間距以沿著滾動方向創(chuàng)建單行items膀捷。items也可以是不同的大小迈嘹,產(chǎn)生比傳統(tǒng)的網(wǎng)格更不對稱的東西,但仍然是一個線性的流水布局全庸。這里可以有很多可能性秀仲。
You can configure the flow layout either programmatically or using Interface Builder in Xcode. The steps for configuring the flow layout are as follows:
- Create a flow layout object and assign it to your collection view.
- Configure the width and height of cells.
- Set the spacing options (as needed) for the lines and items.
- If you want section headers or section footers,specify their size.
- Set the scroll direction for the layout.
你可以以編程方式或使用Xcode中的Interface Builder配置流布局。配置流程布局的步驟如下:
- 創(chuàng)建一個流布局對象壶笼,并將其分配給你的集合視圖神僵。
- 配置cells的寬度和高度。
- 設置行間距和items的間距(根據(jù)需要)覆劈。
- 如果你想要section headers 或 section footers保礼,請指定它們的大小。
- 設置布局的滾動方向责语。
Important: At a minimum, you must specify the width and height of cells. If you don’t, your items are assigned a width and height of 0 and will never be visible.
重要說明: 至少你必須指定cell的寬度和高度炮障。如果你不這樣做,你的items的寬度和高度都被設置為0坤候,并將永遠不可見胁赢。
二、 Customizing the Flow Layout Attributes
The flow layout object exposes several properties for configuring the appearance of your content. When set, these properties are applied to all items equally in the layout. For example, setting the cell size using the itemSize property of the flow layout object causes all cells to have the same size.
If you want to vary the spacing or size of items dynamically, you can do so using the methods of the UICollectionViewDelegateFlowLayout protocol. You implement these methods on the same delegate object you assigned to the collection view itself. If a given method exists, the flow layout object calls that method instead of using the fixed value it has. Your implementation must then return appropriate values for all of the items in the collection view.
流布局對象公開了幾個用于配置內(nèi)容外觀的屬性白筹。當設置時智末,這些屬性將每一個item都相同的大小應用于布局中的所有items谅摄。例如,使用itemSize屬性設置cell大小會導致所有cell具有相同的大小吹害。
如果要動態(tài)改變項目的間距或大小螟凭,可以使用UICollectionViewDelegateFlowLayout
協(xié)議的方法來完成。你可以在賦值給集合視圖本身的同一個delegate對象上實現(xiàn)這些方法它呀。如果給定的方法存在螺男,則流布局對象將調(diào)用該方法,而不是使用它的固定值纵穿。然后下隧,你的實現(xiàn)必須返回集合視圖中所有項目的適當值。
Specifying the Size of Items in the Flow Layout
If all of the items in the collection view are the same size, assign the appropriate width and height values to the itemSize property of the flow layout object. (Always specify the size of items in points.) This is the fastest way to configure the layout object for content whose size does not vary.
If you want to specify different sizes for your cells, you must implement the collectionView:layout:sizeForItemAtIndexPath: method on the collection view delegate. You can use the provided index path information to return the size of the corresponding item. During layout, the flow layout object centers items vertically on the same line, as shown in Figure 3-2. The overall height or width of the line is then determined by the largest item in that dimension.
如果集合視圖中的所有items的大小相同谓媒,則使用itemSize屬性配置流布局對象相應的寬度和高度淆院。(始終以點為單位指定項目的大小。)這是配置布局對象(大小不變的內(nèi)容)的最快方法句惯。
如果要為cells指定不同的大小土辩,則必須實現(xiàn)集合視圖delegate對象的collectionView:layout:sizeForItemAtIndexPath:
方法。你可以使用提供的索引路徑信息返回相應item的大小抢野。在布局過程中拷淘,流布局對象一中心為線垂直放置在同一行上,如圖3-2所示指孤。行的總高度或?qū)挾扔稍摲较蛏系淖畲骾tem確定启涯。
Figure 3-2 Items of different sizes in the flow layout
Note: When you specify different sizes for cells, the number of items on a single line can vary from line to line.
注意: 當為cell指定不同的大小時,單個行上的item數(shù)可能因行而異恃轩。
Specifying the Space Between Items and Lines
Using the flow layout, you can specify the minimum spacing between items on the same line and the minimum spacing between successive lines. Keep in mind that the spacing you provide is only the minimum spacing. Because of how it lays out content, the flow layout object may increase the spacing between items to a value greater than the one you specified. The layout object may similarly increase the actual line-spacing when the items being laid out are different sizes.
During layout, the flow layout object adds items to the current line until there is not enough space left to fit an entire item. If the line is just big enough to fit an integral number of items with no extra space, then the space between the items would be equal to the minimum spacing. If there is extra space at the end of the line, the layout object increases the interitem spacing until the items fit evenly within the line boundaries, as shown in Figure 3-3. Increasing the spacing improves the overall look of the items and prevents large gaps at the end of each line.
使用流布局结洼,可以指定同一行上的items之間的最小間距和連續(xù)行之間的最小間距。請記住叉跛,你提供的間距只是最小間距松忍。由于其布局內(nèi)容的方式,流布局對象可能會將items之間的間距增加到大于你指定的值筷厘。當布置的items是不同尺寸時挽铁,布局對象可能會同樣地增加實際行間距。
在布局過程中敞掘,流布局對象會將items添加到當前行叽掘,直到?jīng)]有足夠的空間來放置整個item。如果這條線足夠大玖雁,可以放入整個數(shù)量的物品而沒有額外的空間更扁,那么物品之間的空間就等于最小間距。如果在行尾有額外的空間,布局對象將增加interitem間距浓镜,直到items在行邊界內(nèi)均勻匹配溃列,如圖3-3所示。增加間距可以改善item的整體外觀膛薛,并防止每條線的末端出現(xiàn)大的間隙听隐。
Figure 3-3 Actual spacing between items may be greater than the minimum
For interline spacing, the flow layout object uses the same technique that it does for inter-item spacing. If all items are the same size, the flow layout is able to respect the minimum line spacing value absolutely and all items in one line appear to be spaced evenly from the items in the next line. If the items are of different sizes, the actual spacing between individual items can vary.
Figure 3-4 demonstrates what happens with the minimum line spacing when items are of different sizes. With differently sized items, the flow layout object picks the item from each line whose dimension in the scrolling direction is the largest. For example, in a vertically scrolling layout, it looks for the item in each line with the greatest height. It then sets the spacing between those items to the minimum value. If the items are on different parts of the line, as shown in the figure, the actual line spacing appears to be greater than the minimum.
對于interline間距,流布局對象使用與項間間距(inter-item)相同的技術(shù)哄啄。如果所有items大小相同雅任,則流程布局能夠絕對地遵守最小行間距值,并且一行中的所有items看起來與下一行中的項目均勻分隔咨跌。如果items的尺寸不同沪么,單個item之間的實際間距可能會有所不同。
圖3-4演示了當項目大小不同時锌半,最小行間距會發(fā)生什么情況禽车。對于不同大小的項目,流布局對象將從滾動方向上維度最大的每行中選取item刊殉。例如殉摔,在垂直滾動布局中,它會查找每個高度最大的行中的item记焊。然后將這些items之間的間距設置為最小值逸月。如果items位于線條的不同部分,如圖所示亚亲,實際的線條間距似乎大于最小值。
Figure 3-4 Line spacing varies if items are of different sizes
As with other flow layout attributes, you can use fixed spacing values or vary the values dynamically. Line and item spacing is handled on a section-by-section basis. Thus, the line and interitem spacing is the same for all of the items in a given section but may vary between sections. You set the spacing statically using the minimumLineSpacing and minimumInteritemSpacing properties of the flow layout object or using the collectionView:layout:minimumLineSpacingForSectionAtIndex: and collectionView:layout:minimumInteritemSpacingForSectionAtIndex: methods of your collection view delegate.
與其他流布局屬性一樣腐缤,你可以使用固定間距值或動態(tài)值捌归。line space和item space是在section與section的基礎上處理的。因此岭粤,給定section中的所有item的line space和interitem相同惜索,但部分之間可能會有所不同。你可以設置靜態(tài)間距使用流動布局對象的minimumLineSpacing和minimumInteritemSpacing屬性剃浇,或使用collectionView:layout:minimumLineSpacingForSectionAtIndex:
與collectionView:layout:minimumInteritemSpacingForSectionAtIndex:
代理方法巾兆。
Using Section Insets to Tweak the Margins of Your Content
Section insets are a way to adjust the space available for laying out cells. You can use insets to insert space after a section’s header view and before its footer view. You can also use insets to insert space around the sides of the content. Figure 3-5 demonstrates how insets affect some content in a vertically scrolling flow layout.
Section insets是調(diào)整已經(jīng)被放置cell的空間的一種方法。你可以使用insets屬性在section的 header view之后和section的 footer view之前插入空間虎囚。你也可以使用insets屬性在內(nèi)容的周圍插入空間角塑。圖3-5演示了insets如何影響垂直滾動流布局中的某些內(nèi)容。
Figure 3-5 Section insets change the available space for laying out cells
Because insets reduce the amount of space available for laying out cells, you can use them to limit the number of cells in a given line. Specifying insets in the nonscrolling direction is one way to constrict the space for each line. If you combine that information with an appropriate cell size, you can control the number of cells on each line.
由于insets減少了可用于布置cell的空間量淘讥,因此可以使用它們來限制給定行中的cells數(shù)量圃伶。在非滾動方向上指定insets是縮小每行的空間的一種方法。如果將這些信息與適當?shù)腸ell大小相結(jié)合,則可以控制每行上的cells數(shù)量窒朋。
三搀罢、 Knowing When to Subclass the Flow Layout
Although you can use the flow layout very effectively without subclassing, there are still times when you might need to subclass to get the behavior you need. Table 3-1 lists some of the scenarios for which subclassing UICollectionViewFlowLayout is necessary to achieve the desired effect.
盡管不用子類化就可以很方便的使用流水布局,但仍然有時候需要子類化來實現(xiàn)你想要的行為侥猩。表格3-1列出了一些場景榔至,要實現(xiàn)希望的效果,子類化UICollectionViewFlowLayout是必須欺劳。
There are also instances in which the right thing to do is to create a custom layout from scratch. Before you decide to do this, take the time to consider whether or not it is really necessary. The flow layout provides a lot of customizable behavior that is appropriate for many different kinds of layouts, and because it is provided to you, it is easy to use and contains numerous optimizations to make it efficient. However, all this is not to say that you should never create a custom layout, because there are circumstances in which doing so make absolute sense. The flow layout limits the scroll direction to one direction, so if your layout contains content that stretches farther than the bounds of the screen in both directions, a custom layout makes more sense to implement. Creating a custom layout is the right decision if your layout is not a grid or a line-based breaking layout, as described above, or if the items within your layout move so frequently that subclassing the flow layout is more compicated than creating your own.
For more on creating a custom layout, see Creating Custom Layouts.
還有一些情況下唧取,正確的做法是從頭開始創(chuàng)建自定義布局。在你決定這樣做之前杰标,花點時間考慮一下是否真的有必要兵怯。流布局提供了許多適合于許多不同類型布局的可定制行為,并且因為它提供給你腔剂,所以它易于使用并包含許多優(yōu)化以使其更高效媒区。然而,這并不是說你永遠不應該創(chuàng)建一個自定義的布局掸犬,因為在某些情況下袜漩,這樣做是絕對有意義的。流布局將滾動方向限制為一個方向湾碎,因此如果你的布局包含的內(nèi)容比屏幕邊界更遠并且是兩個方向的宙攻,則自定義布局更有意義。
有關(guān)創(chuàng)建自定義布局的更多信息介褥,請參閱《Creating Custom Layouts》座掘。