iosipadsplit-screen-multitasking

How to set Notification if view size changed when using multitasking | Swift


When testing my app on an iPad using multitasking and I change the app size on the app, the collection view cells need to be resized or else they don't fit in the view window.

I have took some measures such as

This is all great but however when actually changing the window size when using multitasking, or going back to the full screen app, the cell sizes don't change and look out of place. This is until forced by switching orientation or switching view controllers so viewWillLoad or viewDidLoad gets executed again.

So how can I set a notification if the view.frame size changes ?


Solution

  • I think you are over complicating the issue, just use the following so when the device is rotated or resized the collection view also changes accordingly.

    override func viewWillLayoutSubviews() {
        super.viewWillLayoutSubviews()
    
        guard let collectionLayout = collectionView.collectionViewLayout as? UICollectionViewFlowLayout else {
            return
        }
    
        if UIApplication.shared.statusBarOrientation == UIInterfaceOrientation.landscapeLeft ||
            UIApplication.shared.statusBarOrientation == UIInterfaceOrientation.landscapeRight {
            //here you can do the logic for the cell size if phone is in landscape
        } else {
            //logic if not landscape
        }
    
        collectionLayout.invalidateLayout()
    }