Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Swift viewWillTransition not called

I'm creating a full screen image gallery using a UICollectionView. When the user rotates the device, I perform updates to the UICollectionView within

func viewWillTransition(to size: CGSize, with coordinator: UIViewControllerTransitionCoordinator)

I present this UIViewController modally and have a UICollectionView taking up the full screen. Within viewDidLoad, I create the flow layout as:

let flowLayout = UICollectionViewFlowLayout()
flowLayout.scrollDirection = .horizontal
flowLayout.minimumInteritemSpacing = 0
flowLayout.minimumLineSpacing = 0
photosCollectionView.isPagingEnabled = true
photosCollectionView.setCollectionViewLayout(flowLayout, animated: true)

I also have the size as:

func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
    return photosCollectionView.frame.size
}

When I rotate my device, viewWillTransition(to size: CGSize, with coordinator: UIViewControllerTransitionCoordinator) is never called, which causes the UICollectionViewLayout to not update. While I rotate the device, I do get the message:

The behavior of the UICollectionViewFlowLayout is not defined because: the item height must be less than the height of the UICollectionView minus the section insets top and bottom values, minus the content insets top and bottom values.

I've read online that I can add:

self.automaticallyAdjustsScrollViewInsets = false

to the UIViewController, but that had no affect. There are no content or section insets with the UICollectionView.

I also have the super.viewWillTransition called within the function as well. Can anyone assist me on what could be causing this issue?

like image 540
Mike Walker Avatar asked Feb 10 '17 23:02

Mike Walker


3 Answers

If you are just concern about the layout when the device rotate then please use:

override func viewDidLayoutSubviews() {
    super.viewDidLayoutSubviews()

    collectionView.collectionViewLayout.invalidateLayout()
    collectionView.layoutIfNeeded()
}

From apple docs:

public func viewWillTransition(to size: CGSize, with coordinator: UIViewControllerTransitionCoordinator)

This method is called when the view controller's view's size is changed by its parent (i.e. for the root view controller when its window rotates or is resized). If you override this method, you should either call super to propagate the change to children or manually forward the change to children.

I guess you might called this function on a parent of that view without calling super

A work around would also be to register for the device rotation:

override func viewWillAppear(_ animated: Bool) {
    super.viewWillAppear(animated)

    NotificationCenter.default.addObserver(self, selector: #selector(deviceOrientationDidChange), name: NSNotification.Name.UIDeviceOrientationDidChange, object: nil)
}

override func viewWillDisappear(_ animated: Bool) {
    super.viewWillDisappear(animated)

    NotificationCenter.default.removeObserver(self)
}

@objc func deviceOrientationDidChange(_ notification: Notification) {
    let orientation = UIDevice.current.orientation
    print(orientation)
}
like image 183
zombie Avatar answered Oct 20 '22 00:10

zombie


I'd like to repost an answer which i left in another thread which describes the same issue.


People have already explained that you have to call super. I'd like to add a piece of information that might help people who would have faced what i faced.

Scenario: Parent -> Child (viewWillTransition not called in child)


If your view controller is a child view controller, then check if the parent view controller delegate is called and if super is called there. Otherwise it won't be propagated to the child view controller!

class ParentViewController: UIViewController {

    func presentChild() {
        let child = ChildViewController()
        present(child, animated: false, compeltion: nil)
    }

    override func viewWillTransition(to size: CGSize, with coordinator: UIViewControllerTransitionCoordinator) {
        super.viewWillTransition(to: size, with: coordinator) // If this line is missing your child will not get the delegate call in it's viewWillTransition

        // Do something
    }
}

class ChildViewController: UIViewController {

    // This method will not get called if presented from parent view controller and super is not called inside the viewViewWillTransition available there.
    override func viewWillTransition(to size: CGSize, with coordinator: UIViewControllerTransitionCoordinator) {
       super.viewWillTransition(to: size, with: coordinator)

       //Do something
    }
}

P.S - This happened to me because the code for the parent was written by someone else and they forgot to call super.

like image 25
Rakesha Shastri Avatar answered Oct 20 '22 01:10

Rakesha Shastri


I replaced

func viewWillTransition(to size: CGSize, with coordinator: UIViewControllerTransitionCoordinator)

by

func willTransition(to newCollection: UITraitCollection, with coordinator: UIViewControllerTransitionCoordinator)

and it works for me.

like image 32
David Chea Avatar answered Oct 20 '22 00:10

David Chea