قالب وردپرس درنا توس
Home / IOS Development / # 308: Auto Layout for Layout Transitions with UICollectionViewController 📱➡️📱 – Little Bites of Cocoa

# 308: Auto Layout for Layout Transitions with UICollectionViewController 📱➡️📱 – Little Bites of Cocoa



Subjects

Topics

Today we continue to look at UICollectionView s more advanced capabilities by checking out a known feature that's hidden in UICollectionViewController .

It allows us to automatically animate the transition between the layouts when you press a navigation control unit stack. Neat!

Let's try.

We start with the same simple baseline collection control we used in Bite # 306, and Bite # 307, SquaresViewController ]. It looks like this:

  class    SquaresViewController :    UICollectionViewController    {
    var    items    =    [  Item  ] []] 

    ride    FUNC    collectionView  (
      _    collectionView :    UICollectionView  
     numberOfItemsInSection    section [1
9659017]:
Int ) -> Int {. return items count } ride FUNC collectionView ( _ collectionView : UICollectionView ]      cellForItemAt indexPath : indexPath ) [19659022] -> UICollectionViewCell { la cell = collectionView [19659064] dequeueReusableCell ( withReuseIdentifier : [19659074] "The EmCell"          to . indexPath ) cell background color = elements [ indexPath ] ] . color return cell } }

No fancy, just standard UICollectionViewController ] things.

Next, we will subclass it twice. Once for our "small" cells:

  class    SmallViewController  :    SquaresViewController    {
    init  ()    {
      la    layout    =    UICollectionViewFlowLayout  () 
      layout    itemSize    =    CGSize  (  width :    50 [19659017]    ] :    50 ) 

      super    init  (  collectionViewLayout .    layout ) 

      items  19659022] =    (  0  ...   50 ) .            ( ] 

] : . Random ()) 19659092]] }

We will fill it up with some random items, as we did before, here in our "small" subclassed view controller .

When a user selects one of the cells We push on a "large" display regulator sets its items own:

    overrides func collectionView
_ collectionView: UICollectionView,
didSelectItemAt indexPath: IndexPath
) {
la bigVC = BigViewController ()

bigVC.items = elements

navigation controller?
.pushViewController (bigVC, animated: true)
}
}

Again, nothing really is crazy here. Finally, before we can try it, let's do our BigViewController :

  class    BigViewController  :    SquaresViewController    {
    init  19659019] {
      la    layout    =    UICollectionViewFlowLayout  () 

      layout .   itemSize    =    CGSize  ([19659119] width :    100     height .    100 ) 

           [

] CollectionViewLayout : layout ) }

We want to make the setup's itemSize [19659013] property a little bigger than before, then we override collectionView (_: didSelectItemAt :) again. [19659000] This time we will only call popViewController :

    override    func    collectionView  (
      _    Collec tionView :    UICollectionView  
     didSelectItemAt    indexPath :?.    IndexPath 
  )    {
      navigationController   [19659070]  
    ] ] 
    ] ] 

If We Build and running now, everything works as expected, but there is no fancy transition going on.

[19659000] init Add SmallViewController function, setting useLayoutToLayoutNavigationTransitions to false :

  class    SmallViewController  :    SquaresViewController    {
    init  ()    ] [
      la    layout    =    UICollectionViewFlowLayout  ()     
      layout .   itemSize    =    CGSize  (  width :    50     heights :    50 ). 

      super    init  ([19659011]] collectionViewLayout :    layout ) 

      useLayoutToLayoutNavigationTransitions    =    false 
      //.  ]   // .. 

So, I'll put the same power plug true in BigViewController :

  class    BigViewController [19659092]:    SquaresViewController    {
    init ] {
      la    layout    =    UICollectionViewFlowLayout  () 
      layout .   itemSize    =    CGSize  (  widths :    100     heights .    100 ) 

      super    init  (  collectionViewLayout [19659017]:    layout ) 

      useLayoutToLayoutNavigationTransitions    =    true 
  } 

    //. . 

That's it. UIKit will notice that we have set our BigViewController s useLayoutToLayoutNavigationTransitions to true and will automatically animate the transition between the two .

Note that the automatic transition animation as UIKit provides smart enough to make sure that the selected cell is still visible after we have pushed in. A nice touch.

It's always fun to discover the practical little behavior that hides in UIKit .

Finally, a few important notes: 19659009] First, the same UICollectionView becomes actual reuse when all this magic happens. The printed on display control will not create its own. This may or may not matter to a given app but it is good to know. [19659000] Second, root view controller ( SmallViewController in our case will still be delegate and dataSource when the new display regulator is pressed.

If we needed to change this behavior, we could comply with the UINavigationControllerDelegate protocol and change these values ​​each time another display controls should appear. The code may look like this:

  extension    SquaresViewController  :    UINavigationControllerDelegate    {
    func    navigationController  (
      _    navigation controller ] :    UINavigationController  
     willShow    viewController :    UIViewController  
       animert :    Boolean 
  )    {
      guard    la    squaresVC    =    viewController    som?    SquaresViewController    Other    {   Return  } 

      SquaresVC [19659045].   collectionView  .   Issue    =    SquaresVC 
      SquaresVC .   collectionView  .   dataSource    =    squaresVC 
  } 
} 

It's all for today, having a specific UICollectionView question, would you like to answer? Send it together !.

Download the Xcode project we built in this bit here.


Source link