collection view 1- Using the Flow layout

內(nèi)容來源:https://developer.apple.com/library/content/documentation/WindowsViews/Conceptual/CollectionViewPGforIOS/UsingtheFlowLayout/UsingtheFlowLayout.html#//apple_ref/doc/uid/TP40012334-CH3-SW1

Using the Flow layout

怎樣使用 UICollectionViewFlowLayout

1.創(chuàng)建flow layout 對(duì)象坛梁,并賦值給collection view.

在創(chuàng)建collection view時(shí)客年,有個(gè)初始化方法的形參就是layout:

UICollectionView(frame:CGRect, collectionViewLayout:UICollectionViewLayout)

2.配置cells的高度和寬度往枷。

3.設(shè)置行(lines)和項(xiàng)(items)的最小間隔。(可選的)

4.如果你要section headers和footers,具體說明它們的尺寸锅铅。

5.設(shè)置滾動(dòng)方向。默認(rèn)是垂直方向简烤。

重要說明:你必須說明cells的高寬果覆。否則呜笑,你的items高寬就被賦值為零,那將不可見彻犁。


items 之間的實(shí)際空間大于等于最小值叫胁。


Knowing When to Subclass the Flow Layout

Scenario

Subclassing tips

1.You want to add new supplementary or decoration views to your layout

The standard flow layout class supports only section header and section footer views and no decoration views. To support additional supplementary and decoration views, you need to override the following methods at a minimum:

layoutAttributesForElementsInRect:(required)

layoutAttributesForItemAtIndexPath:(required)

layoutAttributesForSupplementaryViewOfKind:atIndexPath:(to support new supplementary views)

layoutAttributesForDecorationViewOfKind:atIndexPath:(to support new decoration views)

In yourlayoutAttributesForElementsInRect:method, you can callsuperto get the layout attributes for the cells and then add the attributes for any new supplementary or decoration views that are in the specified rectangle. Use the other methods to provide attributes on demand.

For information about providing attributes for views during layout, seeCreating Layout AttributesandProviding Layout Attributes for Items in a Given Rectangle.

2.You want to tweak the layout attributes being returned by the flow layout

Override thelayoutAttributesForElementsInRect:method and any of the methods that return layout attributes. The implementation of your methods should callsuper, modify the attributes provided by the parent class, and then return them.

For in-depth dicussions of what these methods entail, seeCreating Layout AttributesandProviding Layout Attributes for Items in a Given Rectangle.

3.You want to add new layout attributes for your cells and views

Create a custom subclass ofUICollectionViewLayoutAttributesand add whatever properties you need to represent your custom layout information.

SubclassUICollectionViewFlowLayoutand override thelayoutAttributesClassmethod. In your implementation of that method, return your custom subclass.

You should also override thelayoutAttributesForElementsInRect:method, thelayoutAttributesForItemAtIndexPath:method, and any other methods that return layout attributes. In your custom implementations, you should set the values for any custom attributes you defined.

4.You want to specify initial or final locations for items being inserted or deleted

By default, a simple fade animation is created for items being inserted or deleted. To create custom animations, you must override some or all of the following methods:

initialLayoutAttributesForAppearingItemAtIndexPath:

initialLayoutAttributesForAppearingSupplementaryElementOfKind:atIndexPath:

initialLayoutAttributesForAppearingDecorationElementOfKind:atIndexPath:

finalLayoutAttributesForDisappearingItemAtIndexPath:

finalLayoutAttributesForDisappearingSupplementaryElementOfKind:atIndexPath:

finalLayoutAttributesForDisappearingDecorationElementOfKind:atIndexPath:

In your implementations of these methods, specify the attributes you want each view to have prior to being inserted or after they are removed. The flow layout object uses the attributes you provide to animate the insertions and deletions.

If you override these methods, it is also recommended that you override theprepareForCollectionViewUpdates:andfinalizeCollectionViewUpdatesmethods. You can use these methods to track which items are being inserted or deleted during the current cycle.

For more information about how insertions and deletions work, seeMaking Insertion and Deletion Animations More Interesting.

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個(gè)濱河市汞幢,隨后出現(xiàn)的幾起案子驼鹅,更是在濱河造成了極大的恐慌,老刑警劉巖森篷,帶你破解...
    沈念sama閱讀 206,839評(píng)論 6 482
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件输钩,死亡現(xiàn)場(chǎng)離奇詭異,居然都是意外死亡仲智,警方通過查閱死者的電腦和手機(jī)买乃,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 88,543評(píng)論 2 382
  • 文/潘曉璐 我一進(jìn)店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來钓辆,“玉大人剪验,你說我怎么就攤上這事⊙意桑” “怎么了碉咆?”我有些...
    開封第一講書人閱讀 153,116評(píng)論 0 344
  • 文/不壞的土叔 我叫張陵,是天一觀的道長(zhǎng)蛀恩。 經(jīng)常有香客問我,道長(zhǎng)茂浮,這世上最難降的妖魔是什么双谆? 我笑而不...
    開封第一講書人閱讀 55,371評(píng)論 1 279
  • 正文 為了忘掉前任,我火速辦了婚禮席揽,結(jié)果婚禮上顽馋,老公的妹妹穿的比我還像新娘。我一直安慰自己幌羞,他們只是感情好寸谜,可當(dāng)我...
    茶點(diǎn)故事閱讀 64,384評(píng)論 5 374
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著属桦,像睡著了一般熊痴。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上聂宾,一...
    開封第一講書人閱讀 49,111評(píng)論 1 285
  • 那天果善,我揣著相機(jī)與錄音,去河邊找鬼系谐。 笑死巾陕,一個(gè)胖子當(dāng)著我的面吹牛,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播鄙煤,決...
    沈念sama閱讀 38,416評(píng)論 3 400
  • 文/蒼蘭香墨 我猛地睜開眼晾匠,長(zhǎng)吁一口氣:“原來是場(chǎng)噩夢(mèng)啊……” “哼!你這毒婦竟也來了梯刚?” 一聲冷哼從身側(cè)響起凉馆,我...
    開封第一講書人閱讀 37,053評(píng)論 0 259
  • 序言:老撾萬榮一對(duì)情侶失蹤,失蹤者是張志新(化名)和其女友劉穎乾巧,沒想到半個(gè)月后句喜,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 43,558評(píng)論 1 300
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡沟于,尸身上長(zhǎng)有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 36,007評(píng)論 2 325
  • 正文 我和宋清朗相戀三年咳胃,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片旷太。...
    茶點(diǎn)故事閱讀 38,117評(píng)論 1 334
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡展懈,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出供璧,到底是詐尸還是另有隱情存崖,我是刑警寧澤,帶...
    沈念sama閱讀 33,756評(píng)論 4 324
  • 正文 年R本政府宣布睡毒,位于F島的核電站来惧,受9級(jí)特大地震影響,放射性物質(zhì)發(fā)生泄漏演顾。R本人自食惡果不足惜供搀,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 39,324評(píng)論 3 307
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望钠至。 院中可真熱鬧葛虐,春花似錦、人聲如沸棉钧。這莊子的主人今日做“春日...
    開封第一講書人閱讀 30,315評(píng)論 0 19
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽(yáng)宪卿。三九已至的诵,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間愧捕,已是汗流浹背奢驯。 一陣腳步聲響...
    開封第一講書人閱讀 31,539評(píng)論 1 262
  • 我被黑心中介騙來泰國(guó)打工, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留次绘,地道東北人瘪阁。 一個(gè)月前我還...
    沈念sama閱讀 45,578評(píng)論 2 355
  • 正文 我出身青樓撒遣,卻偏偏與公主長(zhǎng)得像,于是被迫代替她去往敵國(guó)和親管跺。 傳聞我的和親對(duì)象是個(gè)殘疾皇子义黎,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 42,877評(píng)論 2 345

推薦閱讀更多精彩內(nèi)容