uiviewcontrollerios8

willTransitionToTraitCollection not get called, why?


I am trying to replace didRotateFromInterfaceOrientation with willTransitionToTraitCollection - in a UIViewController subclass - as doc says, but method is not get called when I am rotating device. Why?


Solution

  • If you're testing this on iPad it has regular trait collections for vertical and horizontal class for both orientations, thus it will not call this method when orientation changed.

    For iPad (and all devices) this will work perfectly:

    override func viewWillTransition(to size: CGSize,
                  with coordinator: any UIViewControllerTransitionCoordinator) {
        super.viewWillTransition(to: size, with: coordinator)
        coordinator.animate(
            alongsideTransition: nil, completion: { _ in
                self.coll.collectionViewLayout.invalidateLayout()
            }
            )
    }