iosbuttonuisplitviewcontrollerviewwilltransitiontosizedisplaymode

Why is viewWillTransitionToSize... not called when displayModeButtonItem triggers splitViewController collapse?


I have a UISplitViewController whose secondary (detail) VC is a UICollectionViewController. I want the cells of the collection to resize based on changes to the size and aspect of the collection view. I trigger this resizing by overriding the UIContentContainer protocol method:

// MARK: - UIContentContainer protocol methods

override
func viewWillTransitionToSize(size: CGSize, withTransitionCoordinator coordinator: UIViewControllerTransitionCoordinator)
{
    setFlowLayoutItemSizeForViewSize(size)
    collectionViewLayout.invalidateLayout()
}

This is called and works just how I want when the device rotates; but this method is not being called when the button supplied by splitViewController?.displayModeButtonItem() is pressed to show or collapse the primary (master) view controller. Since that collapse necessarily changes the size of the secondary (detail) view, I would have though that the viewWillTransitionToSize... method should be called when it is triggered.

So, two questions:

1) Should the viewWillTransitionToSize... method in fact be invoked when the displayModeButtonItem is pressed? If so, I seem to have found a bug.

2) If what I'm seeing is in fact the correct behavior, can anyone suggest a way for my secondary (detail) controller to "know" either when the displayModeButtonItem is pressed, or when its size is changing as a result of that button being pressed?

Thanks!

Carl


Solution

  • 1) Not a bug; displayModeChange is not being treated a sizeTransition

    2) Your UISplitviewController most likely already has a UISplitViewControllerDelegate which can implement the optional:

    splitViewController(_ svc: UISplitViewController,
          willChangeToDisplayMode displayMode: UISplitViewControllerDisplayMode)
    

    method that will get called with UISplitViewControllerDisplayModePrimaryHidden or UISplitViewControllerDisplayModeAllVisible depending on which mode the splitView is switching to.