My Covid-19 lockdown project, or how I got into a custom UICollectionViewLayout and got a ChatLayout

image



Yes Yes. I understand that it is 2020, that all hardcore IOS developers write exclusively in SwiftUIand Combine, and write articles about UIKitsomething like β€œno ice”. However, 2020 turned out to be different from all previous years. Not at all like that.



Therefore, as soon as a full lockdown hit Dublin somewhere in the middle of March, I began to look for something to do with myself on cold rainy evenings. Having pampered with SwiftUIand Combineand, deciding that I absolutely do not want to be a public alpha tester, although I find this all a step in the right direction, I decided to look deeper at what else interested me, but there was always no time to figure it out. At this very moment, the WebSummit company, in which I recently started working, decided to change the provider's chat and I had to dig deeper into the current chat implementation.



MessageKit UI . MessageKit β€” swift , , JSQMessagesViewController. JSQMessagesViewController 5 . , , UIKit , , , swift- . , UI JSQMessagesViewController. 2020-.



MessageKit 2020-. JSQMessagesViewController Objective-C Swift IOS 2009 . , .



, issues, , , β€” .



UIScrollView , , β€œβ€ . 99% , , . β€œβ€ (contentOffset) .



. - UICollectionViewFlowLayout. ?



, UICollectionViewLayout UICollectionViewFlowLayout, MessageKit Auto-Layout . , UICollectionViewFlowLayout .



. UICollectionViewLayout . . UICollectionViewLayout.



.



, ,



. UICollectionViewLayout . . .



, , , , , , . 4 .



UICollectionViewLayout 2 :

initialLayoutAttributesForAppearingItem(at itemIndexPath: IndexPath) -> UICollectionViewLayoutAttributes?

finalLayoutAttributesForDisappearingItem(at itemIndexPath: IndexPath) -> UICollectionViewLayoutAttributes?



initialLayoutAttributesForAppearingItem finalLayoutAttributesForDisappearingItem



initialLayoutAttributesForAppearingItem :



When your app inserts new items into the collection view, the collection view calls this method for each item you insert. Because new items are not yet visible in the collection view, the attributes you return represent the item’s starting state. For example, you might return attributes that position the item offscreen or set its initial alpha to 0. The collection view uses the attributes you return as the starting point for any animations. (The end point of the animation is the item’s new location and attributes.) If you return nil, the layout uses the item’s final attributes for both the start point and end point of the animation.

The default implementation of this method returns nil. Subclasses are expected to override this method, as needed, and provide any initial attributes.

, , , . finalLayoutAttributesForDisappearingItem . ,



If you return nil, the layout uses the item’s final attributes for both the start point and end point of the animation.

.



. . , c itemIndexPath? 0. 0 β€” 1? ? 0 0 2?



, , , . : , , . , IOS 12 IOS 13 . . .



UICollectionViewFlowLayout , ChatLayout, , , , .



UICollectionViewFlowLayout , .



, . / UICollectionViewFlowLayout private API … Apple UIKit. , . , , , . , / UICollectionViewFlowLayout. .



AutoLayout





AutoLayout. . ? ? , ? - -?



UICollectionViewLayout . . , . UICollectionViewFlowLayout , prepare , . , AutoLayout .



, : airbnb/MagazineLayout. . . , initialLayoutAttributesForAppearingItem /finalLayoutAttributesForDisappearingItem , AirBnB.



. , " " β€” ! , .



: UICollectionViewLayoutAttributes, , initialLayoutAttributesForAppearingItem, invalidationContext(forPreferredLayoutAttributes:, withOriginalAttributes originalAttributes:) frame UICollectionView KVO (Key-Value-Observing) . preferredAttributes, originalAttributes. . .





, , . UIScrollView , , , , , .



, . adjustedContextInsets, , .



, UICollectionViewLayout. , . UICollectionViewLayout targetContentOffset(forProposedContentOffset proposedContentOffset: CGPoint) -> CGPoint , UICollectionView : , , , contentOffset , β€” .



, , , , . , UICollectionViewLayoutInvalidationContext contentOffsetAdjustment, . 2 , proposedContentOffset β€” , , finalizeCollectionViewUpdates. .



, , AutoLayout . UICollectionView (contentSize) contentOffsetAdjustment , , : contentSizeAdjustment , contentOffset . .



.



private API





, , . . , , rdar://40926834: Self Sizing + Prefetching Bugs AirBnB. , .



rdar://46865293: Cell's autoresizing mask breaks self-sizing. layoutAttributesForElements(in rect: CGRect) -> [UICollectionViewLayoutAttributes]? , .



, MagazineLayout UICollectionViewFlowLayout _prepareForCollectionViewUpdates:withDataSourceTranslator: prepareForCollectionViewUpdates. Apple, UIKit. : β€” UICollectionView prepareForCollectionViewUpdates. .



IOS , UICollectionView .. .. β€” .



, , . , . , , UIKit . .





, UICollectionView + UICollectionViewLayout . , , β€” . contentInset, , , β€” . β€” . β€” . ? UICollectionViewFlowLayout β€” UICollectionView + UICollectionViewLayout , , " ". , -, , : β€” / ..



?





, , . MessageKit ChatLayout. , MessageKit . .



UIViewController . β€” . , , . , , / master.



MessageKit. . . Jira .



?



ChatLayout.



, , , preview, . , , - , , , , . .



, . .. . : -. . RouteComposer. , , 2 open-source . , , .



, .





ChatLayout β€” UI . , UICollectionViewLayout, , . , Extras UIView, , , - .





  • (UICollectionViewCell) UIView (UICollectionReusableView).
  • /// .
  • contentOffset UICollectionView .
  • .
  • UIView-, .


(, -, )



ChatLayout UICollectionViewLayout, :



  • UIViewController UICollectionView. . ,
  • UICollectionViewCell . , .
  • . ChatLayout . , , . , β€” .
  • . . UICollectionViewDataSource . - DifferenceKit, .
  • ChatLayout . . β€” contentInsets UICollectionView.
  • ChatLayout . - InputBarAccessoryView ( , MessageKit). .


. . .



Oh yes. GIFs. (Caution for epileptics)














All Articles