ios 動態化視圖
by Payal Gupta
通過Payal Gupta
如何在iOS應用中使集合視圖的高度動態化 (How to make height of collection views dynamic in your iOS apps)
充滿活力,就像生活一樣… (Be dynamic, just like life…)
Table views and collection views have always been an integral part of iOS app development. We all might have came across various issues related to them. In this article, we’ll discuss one such problem statement related to collection views.
表格視圖和集合視圖一直是iOS應用程序開發的組成部分。 我們大家可能都遇到過與他們有關的各種問題。 在本文中,我們將討論一種與集合視圖相關的問題陳述。
問題陳述 (Problem Statement)
Let’s assume we’ve a UICollectionView
in a UITableViewCell
and around 20 UICollectionViewCells
that we need to render in it vertically. We can definitely implement that in the blink of an eye with the given data source.
假設我們已經一個UICollectionView
在UITableViewCell
和周圍20 UICollectionViewCells
,我們需要它垂直于渲染。 我們一定可以在給定數據源的眨眼之間實現它。
Now comes the actual problem statement — we need the UITableViewCell
to adjust its height dynamically according to its content. Also, the UICollectionView
must be such that all the cells are displayed in one go, i.e. no scrolling allowed.
現在是實際的問題陳述-我們需要UITableViewCell
根據其內容動態調整其高度。 同樣, UICollectionView
必須確保UICollectionView
顯示所有單元格,即不允許滾動。
Long story short: make everything dynamic…
長話短說: 讓一切充滿活力…
讓我們開始編碼 (Let’s start coding)
A view in iOS calculates its height from its content, given there is no height constraint provided. Same goes for UITableViewCell
.
鑒于沒有提供高度限制,iOS中的視圖根據其內容計算高度。 UITableViewCell
。
For now, we’ll keep a single collectionView
inside our tableViewCell
with its leading, top, trailing and bottom constraints
set to 0.
現在,我們將在tableViewCell
保留一個collectionView
,其leading, top, trailing and bottom constraints
設置為0。
Since we haven’t provided any height constraint to the collectionView
and neither do we know the its contentSize
beforehand, then how will the tableViewCell
calculate its height?
由于我們沒有為collectionView
提供任何高度限制,并且我們也不事先知道其contentSize
,那么tableViewCell
將如何計算其高度?
解 (Solution)
Dynamically calculating the collectionView’s
height as per its contentSize
is simply a 3 step process.
動態計算collectionView's
高度按照其contentSize
是一個簡單的3個步驟。
1. Subclass UICollectionView
and override its layoutSubviews()
and intrinsicContentSize
, i.e.
1.子類化UICollectionView
并覆蓋其layoutSubviews()
和intrinsicContentSize
,即
The above code will invalidate the intrinsicContentSize
and will use the actual contentSize
of collectionView
. The above code takes into consideration the custom layout
as well.
上面的代碼將使intrinsicContentSize
無效,并將使用collectionView
的實際contentSize
。 上面的代碼也考慮了custom layout
。
2. Now, set DynamicHeightCollectionView
as the collectionView’s
class in storyboard
.
2.現在,在storyboard
中將DynamicHeightCollectionView
設置為collectionView's
類。
3. One last thing, for the changes to take effect: you need to call layoutIfNeeded()
on collectionView
, after reloading collectionView’s
data, i.e.
3.最后一件事, layoutIfNeeded()
更改生效:在重新加載collectionView's
數據后,需要在collectionView
上調用layoutIfNeeded()
,即
func configure(with arr: [String]) { self.arr = arr self.collectionView.reloadData() self.collectionView.layoutIfNeeded() //Here..!!!}
And there you have it!
在那里,您擁有了!
樣例項目 (Sample Project)
You can download the sample project from here.
您可以從此處下載示例項目。
進一步閱讀 (Further reading)
Don’t forget to read my other articles:
不要忘記閱讀我的其他文章:
Everything about Codable in Swift 4
Swift 4中有關Codable的一切
Everything you’ve always wanted to know about notifications in iOS
您一直想了解的有關iOS中通知的所有信息
Deep copy vs. shallow copy — and how you can use them in Swift
深層副本與淺層副本-以及如何在Swift中使用它們
Coding for iOS 11: How to drag & drop into collections & tables
iOS 11編碼:如何拖放到集合和表格中
All you need to know about Today Extensions (Widget) in iOS 10
您需要了解的有關iOS 10中的Today Extensions(Widget)的所有信息
UICollectionViewCell selection made easy..!!
UICollectionViewCell選擇變得簡單.. !!
Feel free to leave comments in case you have any questions.
如有任何疑問,請隨時發表評論。
翻譯自: https://www.freecodecamp.org/news/how-to-make-height-collection-views-dynamic-in-your-ios-apps-7d6ca94d2212/
ios 動態化視圖